首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shell脚本对行进行切片,并获得第一部分

Shell脚本对行进行切片,并获得第一部分
EN

Stack Overflow用户
提问于 2016-05-11 05:10:32
回答 2查看 95关注 0票数 0

这里我有一项任务,从有大约1000行的日志文件中提取文件名,日志中的每一行都以文件名开始,后面跟着其他细节,现在我想从每一行中提取每个文件名(绝对路径,从‘./’开始),并将其放入文件中。示例日志文件具有以下数据。

代码语言:javascript
复制
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_overview.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_old_db.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchange.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_channel.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_vhosts.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_permission.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_util.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_purge.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_format.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_exchanges.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_bindings.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_definitions.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-management/src/rabbit_mgmt_wm_queue_get.erl:1:%%   The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-federation-management/src/rabbit_federation_mgmt.erl:1:%%  The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_processor.erl:1:%% The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_util.erl:1:%% The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_collector.erl:1:%% The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_frame.erl:1:%% The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-mqtt/src/rabbit_mqtt_sup.erl:1:%% The contents of this file are subject to the Mozilla Public License
./plugins-src/rabbitmq-mqtt/src/rabbit_mqtt.erl:1:%% The contents of this file are subject to the Mozilla Public License

有一个冒号(:)可以用作分隔符,它精确地结束每一行中的文件名,但我在shell脚本中没有专门知识来分割它并提取文件名。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-11 05:14:22

代码语言:javascript
复制
 awk -F':' '{print $1}' filename.log

 # OR 

 cut -d':' -f1 filename.log
票数 1
EN

Stack Overflow用户

发布于 2016-05-11 05:57:11

使用bash的另一个途径是:

代码语言:javascript
复制
while read -r line; do echo "${line%%:*}"; done <filename

它使用参数展开和子字符串删除,这是一组内置字符处理例程。基本上:

代码语言:javascript
复制
var="123:456:789"
echo "${var#*:}"   # 456:789  remove from left to 1st occurrence of ':'
echo "${var%:*}"   # 123:456  remove from right to 1st occurrence of ':'
echo "${var##*:}"  # 789  remove from left to last occurrence of ':'
echo "${var%%:*}"  # 123  remove from right to last occurrence of ':'

(注意:通配符在展开中的位置)

它们甚至可以嵌套。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37153545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档