1

I just want to share a problem I had this morning using grep -f to obtain the patterns to match from a text file.

Symptom

$ echo 'sandbox/test/script.py' | grep -G '^sandbox/'
sandbox/test/script.py

Using -f option fails:

$ echo 'sandbox/test/script.py' | grep -G -f patterns.txt
$ cat patterns.txt
^sandbox/

I didn't understand why grep was failing until I inspected the text file:

$ file patterns.txt
/home/miguel/patterns.txt: ASCII text, with CRLF line terminators

This file was being used by different colleagues on different Operating Systems, and someone saved it using the Windows line terminators.

  • 2
    dos2unix will fix it. – Archemar Dec 28 '15 at 15:21
  • Yes, check the solution below. – Miguel Rentes Dec 28 '15 at 15:38
  • -f means fixed patterns, no regexps (and no anchors either). – vonbrand Dec 28 '15 at 16:28
  • That is why I used the -G flag, which interprets a pattern (obtained from the text file) as a basic regular expression. The -f is for obtaining the patterns from a text file, one per line. – Miguel Rentes Dec 28 '15 at 16:34
  • @vonbrand, I think you have -f and -F mixed up – iruvar Dec 28 '15 at 17:23
3

Solution

Using dos2unix solved the problem.

$ dos2unix patterns.txt
dos2unix: converting file /home/miguel/patterns.txt to Unix format ...
$ file patterns.txt
/home/miguel/patterns.txt: ASCII text

And now grep -f works again.

$ echo 'sandbox/test/script.py' | grep -G -f patterns.txt
sandbox/test/script.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.