Karthic.K Karthic.K - 7 months ago 15
Bash Question

Shell script to slice lines and get the first part

I've a task here to extract the file names from a log file which has about 1000 lines, in the log every line starts with the file name followed by other details, I now want to extract every file name(absolute path, starting from './') from each line and put that in a file. A sample log file has the below data.

./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


there is a colon(:) which can be used as a delimiter that exactly ends the file name in each line, but I've no expertise in shell script to slice it and extract the file names.

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

 # OR 

 cut -d':' -f1 filename.log