3

Is there a way to select only those lines from a text file which exclude a substring?For example, I have the file:

I have a /apple
I have a peach
I have a cucumber/strawberry
ABCDE
I have a apple/grape
XYZEF
POLSD

and i want to select the lines that start with 'I have a' but don't include the string 'apple'.So basically I want to select only:

I have a peach
I have a cucumber/strawberry
mark.herp
  • 31
  • 2

4 Answers4

3

Use a negative lookahead.

^I have a (?!.*\bapple\b).+

will match all "I have a" strings except ones that contain the entire word "apple". This will still match the phrase

I have a pineapple

because of the word boundaries around "apple".

Jongware
  • 22,200
  • 8
  • 54
  • 100
1

Use a pipe:

grep '^I have a' file.txt | grep -v apple
Danny Daglas
  • 1,501
  • 1
  • 9
  • 9
0

You should use the regular expression "starts with" with a multiline flag like this: "^I have a.*" /gm.

Artak
  • 2,819
  • 20
  • 31
0

For a pure regex solution, you can use something like:

^I have a (?!.*apple) - Note: You can also add \b word boundaries around the "apple", like Jongware suggested, if you don't want to match things like "pineapple".

However, a much easier way to do this (especially if the problem was a bit more complicated!) would be to combine a search for the first pattern, with a search excluding the second pattern.

With grep, as suggested by Danny Daglas, you can do grep '^I have a' file.txt | grep -v apple (where grep -v is an "inverse grep").

However, you wanted to do this inside a text editor, which is fair enough. The exact method obviously varies between text editors, but for example for Vim check out this excellent stackoverflow answer.

Community
  • 1
  • 1
Tom Lord
  • 27,404
  • 4
  • 50
  • 77