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

Unix 如何在grep中打印每个匹配行内的列偏移量,unix,grep,Unix,Grep,通过将-o-n传递给grep,我可以输出文件中模式的所有匹配部分,以及找到每个匹配的行号 我怎样才能在找到图案的那一行内打印列偏移量?我想我能够模仿您使用awk所做的相同操作。参考AWK手册: 以下是我的文件的外观: 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

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


我怎样才能在找到图案的那一行内打印列偏移量?

我想我能够模仿您使用awk所做的相同操作。参考AWK手册:

以下是我的文件的外观:

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

我做得又快又脏,但我希望它能帮助你达到你需要的位置。

为了清楚起见,你说的列偏移是指行/字符串中的字符偏移,对吗?是的,对,行中的字符偏移。Vim
set grepprg
:-)你可能需要一个while循环?否则,每行只需找到第一个匹配项。