2016-05-11 26 views
0

我在这里已经一个任务来提取其具有约1000行日志文件的文件名,日志中的每一行开头的文件名,然后其他的细节,我现在要从每行中提取每个文件名(绝对路径,从'./'开始),并将其放入一个文件中。示例日志文件具有以下数据。shell脚本切片线,并获得第一部分

./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脚本切片,并提取文件名。

回答

1
awk -F':' '{print $1}' filename.log 

# OR 

cut -d':' -f1 filename.log 
+0

这是这么简单! ...谢谢你...... :)它拯救了我的一天。 –

+0

很高兴听到,如果有帮助,请接受答案。 –

1

使用bash的另一种途径是:

while read -r line; do echo "${line%%:*}"; done <filename 

它使用以substring去除参数扩展它是一组内置字符处理例程。基本上是:

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 ':' 

注:在扩张的通配符的位置)

他们甚至可以被嵌套为好。