SED和grep得到行号进行匹配 [英] sed and grep get the line number for a match
本文介绍了SED和grep得到行号进行匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个日志文件,我用sed提取其中包含字匹配两个字符串之间的界限。我用sed提取线和grep只服用含有单词MATCH的佳句。我需要在找到匹配的日志文件中的行数。
日期:...
TST STARTS
DISCARD STR1
DISCARD STR2
MATCH STR3 // 5号线
MATCH STR4 // 6号线
STR5 DISCARD
TST FINISHED
我用这个命令来提取行:
SED -n/ TST开始/,/ TST FINISHED / P的log.txt | grep的一致。
我的输出是:
MATCH STR3
MATCH STR4
但我还需要在输出行号:
5号线:MATCH STR3
6号线:MATCH STR4
解决方案
您可以使用:
猫-n
在申请之前你的sed,像这样数的行:
猫-n log.txt中| SED -n/ TST开始/,/ TST FINISHED / P| grep的一致
另外,你可以用的一个变体代替
:猫-n
NL
NL -n LN的log.txt | SED ...
I have a log file and i use sed to extract the lines between two strings which contains the word MATCH. I use sed to extract the lines and grep to only take the lines containing the word "MATCH". I would need the lines number in the log file where a match is found.
Date:...
TST STARTS
DISCARD str1
DISCARD str2
MATCH str3 //line 5
MATCH str4 //line 6
DISCARD str5
TST FINISHED
I use this command to extract the lines:
sed -n "/TST STARTS/,/TST FINISHED/p" log.txt | grep "MATCHED".
My output is:
MATCH str3
MATCH str4
But I also need in the output the lines number:
line 5: MATCH str3
line 6: MATCH str4
解决方案
You could use:
cat -n
to number the lines before you apply your sed, like this:
cat -n log.txt | sed -n "/TST STARTS/,/TST FINISHED/p" | grep "MATCHED"
Alternatively, you could replace cat -n
with a variant of nl
:
nl -n ln log.txt | sed ...
这篇关于SED和grep得到行号进行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文