Monday, April 4, 2022

[SOLVED] How to print the starting position of pattern in grep

Issue

In python's regex (re) library I can do re.search("<pattern>", string).start() to get the start of the pattern (if pattern exists).

How can I do the same in the unix command line tool grep?

E.g. If pattern= "th.n" and the string is "somethingwrong", I expect to see the number 5 (considering 1-based but 4 in a 0-based would be ok)

Thank you!


Solution

Maybe a Perl one-liner would be a happy medium between having to write a Python program and the simplicity of a standard Unix tool.

Given this file:

$ cat foo.txt
This thing
that thing
Not here
another thing way over here that has another thing and a third thing
thank you.

You could run this Perl one-liner:

$ perl -lne'while(/th.n/g){print $.," ",$-[0]," ",$_;}' foo.txt
1 5 This thing
2 5 that thing
4 8 another thing way over here that has another thing and a third thing
4 45 another thing way over here that has another thing and a third thing
4 63 another thing way over here that has another thing and a third thing
5 0 thank you.

Also, the greplike search tool ack (that I wrote)has a --column option to display the column:

$ ack th.n --column foo.txt /dev/null
foo.txt
1:6:This thing
2:6:that thing
4:9:another thing way over here that has another thing and a third thing
5:1:thank you.

Or with the --nogroup option so the filename appears on each line.

$ ack th.n --column --nogroup foo.txt /dev/null
foo.txt:1:6:This thing
foo.txt:2:6:that thing
foo.txt:4:9:another thing way over here that has another thing and a third thing
foo.txt:5:1:thank you.

I had to add the search of /dev/null because ack's output would be different if there was only one file being searched.

ripgrep has a --column option, too.

$ rg --column --line-number th.n foo.txt
1:6:This thing
2:6:that thing
4:9:another thing way over here that has another thing and a third thing
5:1:thank you.


Answered By - Andy Lester
Answer Checked By - Mildred Charles (WPSolving Admin)