Issue
I'm working on a Linux system and need assistance in finding specific lines within a large text file using the grep
command.
The criteria for these lines are:
- Inclusion of either 'term1' or 'term2'.
- Exclusion of 'term3'.
- Ending with 'term4' or 'term5'.
I've been trying different grep
variations but can't seem to fulfill all conditions at once. Could someone help me construct the appropriate grep
command? Your assistance is much appreciated!
Solution
You can use pcre of grep with lookahead assertions :
grep -P '^(?=.*term[12])(?!.*term3).*term[45]$' <<< 'term2 term1 term5'
Answered By - Philippe Answer Checked By - Mildred Charles (WPSolving Admin)