Search code examples
bashgrepcygwingnu

Grep - Getting the character position in the line of each occurrence


According to the manual, the option -b can give the byte offset of a given occurence, but it seems to start from the beginning of the parsed content.

I need to retrieve the position of each matching content returned by grep. I used this line, but it's quite ugly:

grep '<REGEXP>' | while read -r line ; do echo $line | grep -bo '<REGEXP>' ; done

How to get it done in a more elegant way, with a more efficient use of GNU utils?

Example:

$ echo "abcdefg abcdefg" > test.txt
$ grep 'efg' | while read -r line ; do echo $line | grep -bo 'efg' ; done < test.txt
4:efg
12:efg

(Indeed, this command line doesn't output the line number, but it's not difficult to add it.)


Solution

  • Perl is not a GNU util, but can solve your problem nicely:

    perl -nle 'print "$.:$-[0]" while /efg/g'