Dagang Wei
Dagang Wei

Reputation: 26468

sed/awk: Extract pattern from 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 ...

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

server_log_1.log
server_log_2.log
server_log_3.log

Could someone help? Thanks!

The file name pattern is server_log_xxx.log, and it only occurs once in a line.

Upvotes: 13

Views: 41440

Answers (4)

Paweł Nadolski
Paweł Nadolski

Reputation: 8474

Pipe your file through following command:

sed 's/.*\(server_log_[0-9]\+\.log\).*/\1/'

Upvotes: 9

glenn jackman
glenn jackman

Reputation: 246764

Assuming the "xxx" placeholder is only digits:

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

Upvotes: 27

Zsolt Botykai
Zsolt Botykai

Reputation: 51593

With awk and your input pattern:

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

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

HTH

Upvotes: 1

Dimitre Radoulov
Dimitre Radoulov

Reputation: 27990

sed 's|.*/\([^/ ]*\).*|\1|' infile

Upvotes: 0

Related Questions