Ubuntu – How to grep lines, based on a certain pattern

greptext processing

Let's say I have a file containing following two lines:

2014-05-05      09:11:53    /aa/bbbb/cccccc             29899
2014-05-05      09:12:17    /aa/bbbb/cccccc?dddddddd    16767 

I need to get the line containing the pattern /aa/bbbb/cccccc only, I don't need the second line containing extra characters i.e. ?dddddddd. Now when I tried

grep '/aa/bbbb/cccccc' file

Then both of the lines being selected. I need the full line so grep -o could not be a solution.

What could be the possible solution using grep so that only the first line gets selected based on the search pattern?

Best Answer

Try the below grep command which uses -P (Perl-regexp) parameter.

grep -P '(?<!\S)/aa/bbbb/cccccc(?!\S)' file
  • (?<!\S) This negative lookbehind asserts that the character which preceeds the string /aa/bbbb/cccccc would be any but not a non-space character.

  • (?!\S) Negative lookahead asserts that the character following the match would be any but not a non-space character.

Another grep,

 grep -E '(^|\s)/aa/bbbb/cccccc(\s|$)' file

Through python,

script.py

#!/usr/bin/python3
import re
import sys
file = sys.argv[1]
with open(file, 'r') as f:
    for line in f:
        for i in line.split():
            if i == "/aa/bbbb/cccccc":
                print(line, end='')

Save the above code in a file and name it as script.py. Then execute the above script by

python3 script.py /path/to/the/file/you/want/to/work/with
Related Question