How to print column offset in each corresponding row in grep

By passing -o -n to grep, I can output all the relevant parts of the template to a file and the line number on which each match was found.

How can I print the column offset in the row where the template was found?

+6
source share
1 answer

I think I managed to imitate what you do with awk. AWK Guide Link:

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

This is what my file looks like:

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 

And here is the command I ran:

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

And the conclusion:

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

I did it fast and dirty, but I hope this helps you get what you need.

+5
source

Source: https://habr.com/ru/post/944533/


All Articles