简体   繁体   English

Linux命令行帮助| 从文件中提取特定部分

[英]Linux Command line Help | extract specific parts from file

Suppose I have a log which has data in the format given below 假设我有一个日志,其中的数据格式如下

        Time           number status  
2013-5-10 19:18:43.430 123456 success 
2013-5-10 19:28:13.430 134324 fail 
2013-5-10 19:58:33.430 456456 success 

I want to extract the numbers having success status. 我想提取具有success状态的numbers Is there any way in linux using command line(grep, sed) to extract the data as mentioned. 在Linux中,有什么方法可以使用命令行(grep,sed)来提取数据。 ?? ?? Thanks all .. 谢谢大家..

grep only solution: 仅grep解决方案:

grep -Po '\d+(?= success)' file

or with awk only : 使用awk

awk '$4=="success"&&$0=$3' input
cat file | grep success | awk '{print $3}'

你可以做

(grep 'success' | cut -d ' ' -f 3) <$file

这将根据成功状态打印数字:

 awk '$4 ~ /success/ {print $3}' logfile

使用perl:

perl -ne '/success/ && split && print "$_[2]\n"' inputfile

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

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