简体   繁体   中英

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.

假设“xxx”占位符仅为数字:

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

通过以下命令管道文件:

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

With awk and your input pattern:

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

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

HTH

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM