0

I want to extract from a file all lines which don't content a specific pattern.

pattern="tmp/[...]/include/linux/*.h"
file contains:
17937;/home/[...]/tmp/[...]/include/linux/header.h;484;16;[Other text here]
37417;/home/[...]/tmp/[...]/src/file.cpp;1851;41;[Other text here]
17945;/home/[...]/tmp/[...]/include/linux/*/header.h;484;16;[Other text here]

The expected result is only the second line.

I've tried the bellow line and I get the entire content of the file:

grep -E -v $pattern file

It works only with the bellow line, but it is not enough:

grep -E -v .h file

How can I obtain the desired output?

Ciprian Vintea
  • 448
  • 2
  • 4
  • 18
  • 2
    grep -E -v "tmp/\\[...\\]/include/linux/.*\.h" will get the desired result based on your data. – sigmalha Apr 12 '16 at 15:05
  • Is not clear to me if the pattern that determines the lines be excluded is fixed, or is like using a wildcard: *.h -> exclude all files with .h extension. – MarcM Apr 12 '16 at 15:06
  • It is like using a wildcard. All lines with that pattern to be excluded (all .h lines with the same location) – Ciprian Vintea Apr 12 '16 at 15:09
  • Then you should take a look at http://stackoverflow.com/questions/1069302/using-the-star-sign-in-grep – MarcM Apr 12 '16 at 15:09

1 Answers1

1

Your regex needs to become "tmp/\[\.\.\.]/include/linux/.*\.h"

  • The first square bracket need to be escaped, because having a list of characters between [] means "match one of those characters".
  • The wildcard is associated with the character preceding it, so you need to put a dot before, to match any character.
  • And you need to escape the dots if you want to match on them, otherwise they will match any character (it will work in your case though).
Sylvain B
  • 513
  • 7
  • 12
  • 1
    Escaping the first square bracket is also important. See the section on `bracket expression` in the POSIX reg ex man page: `man re_format`. – Eric Bolinger Apr 12 '16 at 16:02