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.(?<!\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,
Through python,
script.py
Save the above code in a file and name it as
script.py
. Then execute the above script by