Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep Regex: List all lines except

Tags:

regex

I'm trying to automagically remove all lines from a text file that contains a letter "T" that is not immediately followed by a "H". I've been using grep and sending the output to another file, but I can't come up with the magic regex that will help me do this.

I don't mind using awk, sed, or some other linux tool if grep isn't the right tool to be using.

like image 687
Matt Parkins Avatar asked Apr 17 '12 10:04

Matt Parkins


People also ask

How do you grep everything except?

How to Exclude a Single Word with grep. The most simple way to exclude lines with a string or syntax match is by using grep and the -v flag. The output will be the example. txt text file but excluding any line that contains a string match with “ThisWord”.

How do you exclude lines in grep?

To exclude particular words or lines, use the –invert-match option. Use grep -v as a shorter alternative. Exclude multiple words with grep by adding -E and use a pipe (|) to define the specific words.

How do I make grep negative?

To use negative matching in grep , you should execute the command with the -v or --invert-match flags. This will print only the lines that don't match the pattern given.


1 Answers

That should do it:

grep -v 'T[^H]' 

-v : print lines not matching

[^H]: matches any character but H

like image 184
byrondrossos Avatar answered Oct 04 '22 19:10

byrondrossos