【问题标题】:Shell script to slice lines and get the first part用于分割线并获取第一部分的 Shell 脚本
【发布时间】:2016-05-11 13:57:11
【问题描述】:

我的任务是从大约 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 脚本对它进行切片和提取文件名的专业知识。

【问题讨论】:

    标签: shell slice delimiter


    【解决方案1】:
     awk -F':' '{print $1}' filename.log
    
     # OR 
    
     cut -d':' -f1 filename.log
    

    【讨论】:

    • 很高兴听到这个消息,如果有帮助,请接受答案。
    【解决方案2】:

    另一个使用 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 ':'
    

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

    它们甚至可以嵌套。

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 2016-06-03
      • 2014-07-24
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多