Sed / awk: Extract a template from a text stream

2011-07-01 ... /home/todd/logs/server_log_1.log ... 2011-07-02 ... /home/todd/logs/server_log_2.log ... 2011-07-03 ... /home/todd/logs/server_log_3.log ... 

My file looks like above. I want to extract the file names from it and output it to STDOUT as:

 server_log_1.log server_log_2.log server_log_3.log 

Can anyone help? Thanks!

The file name template is server_log_xxx.log, and it occurs only once per line.

+7
source share
4 answers

Assuming the placeholder "xxx" is just the numbers:

 grep -o 'server_log_[0-9]\+\.log' 
+16
source

Join the file with the following command:

 sed 's/.*\(server_log_[0-9]\+\.log\).*/\1/' 
+3
source
 sed 's|.*/\([^/ ]*\).*|\1|' infile 
0
source

With awk and your input pattern:

 awk 'BEGIN {FS="/"} { print gensub(" .*$","","g",$5) }' INPUTFILE 

See the action here: https://ideone.com/kcadh

NTN

0
source

All Articles