Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there any way to negate a regular expression so that a search finds everything that does NOT match the pattern?
Also, is there any way to negate a backreference in order to see if two words do NOT match within a line?
For example, the first line has two matching words with a space in between and the second has two non-matching words. The search would match the second one.Bob Bob
Tom CalIf either of these are possible, I think they can accomplish many of the same tasks.
Thank ahead of time,
Carl

For grep, the -v option will negate (or reverse) the result set.
grep red
grep -v redgrep -e red -e blue -e pink
grep -ve red -e blue -e pinksed can negate a pattern with ! The first sed below outputs all lines containing "red", while the second outputs the opposite set of lines.
sed /red/!d
sed /red/dOr we can negate at a higher level. sed -n will suppress sed's default action of outputting each (non-deleted) line. Now, we can print just the ones we want. The following two seds give identical results as the previous two seds:
sed -n /red/p
sed -n /red/!pTo locate lines that have non-matching successive words, you would not negate a back-reference. Locate lines that DO have matching successive words and take appropriate action. The following (requires GNU sed) locates lines containing a string followed by the identical string, separated by one or more spaces. In this case, it deletes the lines, thus giving you all lines that do not.
sed '/\([^ ][^ ]*\) *\1/d'
But the code above is not "word" oriented. It would suppress, for example "Robert Roberta" because it would see the string Robert followed by Robert.
The following code will do the same, word-oriented, insisting that the duplicated string is delimited by spaces or beginning of line or end of line (maybe someone will post a simplified version of this):
sed '
/\(^[^ ][^ ]*\) *\1$/d
/\(^[^ ][^ ]*\) *\1 /d
/\( [^ ][^ ]*\) *\1$/d
/\( [^ ][^ ]*\) *\1 /d
' myfileIf you want to limit your duplicate check to words of known position, awk would be best for that. The following awk will print each line where word3 does not equal word4:
awk '$3!=$4' myfile
And with just a bit more coding, awk can confirm if the line contains (or does not contain) two consecutive identical words.

Thanks for the detailed response! I need to learn more about grep, sed, and awk, but what you have told me works for what I'm doing now. So far, I've just been using vi.
Thanks again,
Carl

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |