Issue
I have a input file such as
file;14;19;;;hello 2019
file2;2019;2020;;;this is a test 2020
file3;25;31;this is a number 31
I would like to grep numbers only after ;;;
. For example if I wanted to grep 2019
it would give me
file;14;19;;;hello 2019
instead of if I did grep '2019' file
file;14;19;;;hello 2019
file2;2019;2020;;;this is a test 2020
How can I accomplish this task?
Solution
Regular expression can include stuff other than fixed text, it sounds like all you need is:
grep ';;;.*[0-9]' inputFile.txt
This will deliver all lines that have the text ;;;
followed by a digit somewhere after that in the line. In terms of explanation:
;;;
is the literal text, three semicolons;.*
is zero or more of any character;[0-9]
is any digit.
That will give you lines with any number. If you want a specific number, use that for the final bullet point above.
Just keep in mind that this will also give you the line xyzzy ;;; 920194
if you go looking for 2019
.
If you want just the 2019
numbers (i.e., without any digits on either side), you can use the zero-width negative look-behind and look-ahead assertions, assuming your version of grep
has Perl-compatible regular expressions (PCRE, which GNU grep
does with the -P
flag):
grep -P ';;;.*(?<![0-9])2019(?![0-9])' inputFile.txt
This can be read as:
;;;
is the literal text, three semicolons;.*
is zero or more of any character;(?<![0-9])
means next match cannot be preceded by a digit;2019
is the number you're looking for;(?![0-9])
means previous match cannot be followed by a digit.
Answered By - paxdiablo