2016-05-11 14 views
0

Ich habe hier eine Aufgabe, um die Dateinamen aus einer Protokolldatei, die etwa 1000 Zeilen enthält extrahieren, im Protokoll beginnt jede Zeile mit dem Dateinamen gefolgt von anderen Details, I Nun möchte ich jeden Dateinamen (absoluter Pfad, beginnend mit './') aus jeder Zeile extrahieren und in eine Datei einfügen. Eine Beispielprotokolldatei enthält die folgenden Daten.Shell-Skript, um Zeilen zu schneiden und den ersten Teil

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

gibt es einen Doppelpunkt (:), die als Trennzeichen verwendet werden kann, die genau die Dateinamen in jeder Zeile enden, aber ich habe keine Erfahrung in Shell-Skript es in Scheiben schneiden und die Dateinamen zu extrahieren.

Antwort

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

# OR 

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

war es so einfach !!! ... danke ... :) es hat mir den Tag gerettet. –

+0

Froh, das zu hören, wenn es dann geholfen hat, bitte akzeptieren Sie die Antwort. –

1

Ein anderer Weg bash verwenden wäre:

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

Es verwendet Parameter Expansion mit Teilzeichenfolge Entfernung, die eine Reihe von eingebauten Zeichenbehandlungsroutinen ist. Grundsätzlich gilt:

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

(Anmerkung: die Position des Wildcard in den Erweiterungen)

Sie können sogar auch verschachtelt werden.

Verwandte Themen