2016-05-11 24 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 ':' 

注:在擴張的通配符的位置)

他們甚至可以被嵌套爲好。