简体   繁体   English

sed / awk:从文本流中提取模式

[英]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: 我想从中提取文件名并输出到STDOUT:

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. 文件名模式是server_log_xxx.log,它只在一行中出现一次。

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

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

通过以下命令管道文件:

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

With awk and your input pattern: 使用awk和您的输入模式:

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

See it action here: https://ideone.com/kcadh 请在此处查看操作: https//ideone.com/kcadh

HTH HTH

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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