Shell 分割行并获取第一部分的脚本
Shell script to slice lines and get the first part
我这里的任务是从一个大约有 1000 行的日志文件中提取文件名,在日志中,每一行都以文件名开头,后跟其他详细信息,我现在想提取每个文件名(绝对路径,从每一行的 './') 开始并将其放入文件中。示例日志文件包含以下数据。
./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 脚本的专业知识来分割它并提取文件名.
awk -F':' '{print }' filename.log
# OR
cut -d':' -f1 filename.log
另一个使用 bash 的途径是:
while read -r line; do echo "${line%%:*}"; done <filename
它使用带子字符串删除的参数扩展,这是一组内置的字符处理例程。基本上:
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 ':'
(注意:扩展中通配符的位置)
它们甚至可以嵌套。
我这里的任务是从一个大约有 1000 行的日志文件中提取文件名,在日志中,每一行都以文件名开头,后跟其他详细信息,我现在想提取每个文件名(绝对路径,从每一行的 './') 开始并将其放入文件中。示例日志文件包含以下数据。
./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 脚本的专业知识来分割它并提取文件名.
awk -F':' '{print }' filename.log
# OR
cut -d':' -f1 filename.log
另一个使用 bash 的途径是:
while read -r line; do echo "${line%%:*}"; done <filename
它使用带子字符串删除的参数扩展,这是一组内置的字符处理例程。基本上:
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 ':'
(注意:扩展中通配符的位置)
它们甚至可以嵌套。