如何在grep中的每个匹配行中打印列偏移量

时间:2013-05-08 14:38:45

标签: unix grep

通过将-o -n传递给grep,我可以输出文件中模式的所有匹配部分,以及找到每个匹配的行号。

如何在该行找到模式的行中打印列偏移量?

1 个答案:

答案 0 :(得分:6)

我想我能够模仿你用awk做的同样的事情。参考AWK手册:

http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_92.html

这是我的文件的样子:

this,is,a,test,line
this, ,a,test,line,with,the,second,field,blank
this, is,another,test,line,with,a,blank,in,the,second,field,but,the,field,isnt,blank
this, ,is,another,line,with,a,blank,second,field

这是我跑的命令:

awk '{regex = "test"; where = match($0, regex); print "REGEX: ",where," on line ",NR}' test

输出:

REGEX:  11  on line  1
REGEX:  10  on line  2
REGEX:  18  on line  3
REGEX:  0  on line  4

我做得很快又脏,但我希望它足以让你到达你需要的地方。