2

I have a requirement where I have to get the file name, line number, match position (byte offset) of a pattern in a file.

I am using the grep command for this and so far am able to get the file name and line number without issues, but un able to get the match position on each line individually.

There is a -b option in grep which gives byte offset but its not giving result for each line individually.

Tried below:

grep --with-filename -n -r -E -o "pattern" file.txt

This gives file name and line number along with match line.

grep --with-filename -n -r -E -o -b "pattern" file.txt

This gives byte offset (position of match in line) but it is considering whole file as a single line and giving result, but I need position result for each line starting with that line not beginning of file.

Eg: file.txt

abc
def
xyzva

Search pattern: a

Expected result:

file.txt:1:0:abc
file.txt:3:4:xyzva

If there is any other solution in Python for the same, that too is acceptable.

1 Answers1

1

If you are okay with position starting with 1 (as opposed to 0), you can use ripgrep

$ cat ip.txt
abc
def
xyzva

$ rg -H --column --no-heading 'a' ip.txt
ip.txt:1:1:abc
ip.txt:3:5:xyzva

You can use awk for a more customizable solution:

$ awk 'match($0, /a/){print FILENAME, NR, RSTART, $0}' OFS=: ip.txt
ip.txt:1:1:abc
ip.txt:3:5:xyzva

$ awk 'match($0, /a/){print FILENAME, NR, RSTART-1, $0}' OFS=: ip.txt
ip.txt:1:0:abc
ip.txt:3:4:xyzva
Sundeep
  • 23,246
  • 2
  • 28
  • 103