6

I have a pattern file, say t.txt, containing the following:

vali 

and the file I want to run grep against, say a, containing the following:

validate validate: bw_validate: [echo] Validating project: CrossService_v1_59_5_1 [echo] Found 62 errors and 28 warnings [echo] ----------------------------------------------------------------- [echo] Validating project: CRM-UDB_59_4_2 [echo] Found 25 errors and 28 warnings [echo] ----------------------------------------------------------------- [echo] Validation Failed: At least one project contains errors. notify: BUILD FAILED bw.xml:311: Validation Failed: At least one project contains errors. 

if I execute:

grep -iE vali a 

I get the expected output, i.e.:

validate validate: bw_validate: [echo] Validating project: CrossService_v1_59_5_1 [echo] Validating project: CRM-UDB_59_4_2 [echo] Validation Failed: At least one project contains errors. bw.xml:311: Validation Failed: At least one project contains errors. 

but if I execute:

grep -iE -f t.txt a 

I don't get any match. files are readable and both in the same directory (from which I execute the command). I tried both with -f and --file=t.txt, --file='t.txt', --file="t.txt"

I'm on linux Fedora 16 64bit. Strangely enough, the same command works properly in windows with the grep/egrep porting.

Am I missing something? Any help is appreciated as this is giving me an headache :( thanks!

4
  • 1
    Can you please do cat -vet t.txt. Looks like your pattern file may contains some spaces or special characters in them. Commented Jan 14, 2012 at 18:25
  • You are right, it showed some extra ^M right before the end of the line. converting it with dos2unix solved it. Commented Jan 15, 2012 at 10:09
  • Cool, I have posted an answer just for reference incase if you ever happen to work on a system that does not have dos2unix available. Good luck!! Commented Jan 15, 2012 at 10:36
  • I was also having problems with DOS newlines, it wasn't failing but was only matching against the final line of the pattern file. The command worked properly after I ran dos2unix. Commented Dec 4, 2013 at 19:02

2 Answers 2

12

It should work right (it works for me). The -f switch is specified by POSIX.

The fact that you say it's working on Windows gave me an idea: Could it be the t.txt file ends with a DOS newline? I tried it with a clean file (no newline), and it worked. Then I tried it with a DOS file, and I was able to reproduce your results.

Try dos2unix to "fix" DOS files.

Sign up to request clarification or add additional context in comments.

Comments

2

If you don't have the dos2unix utility then you can do any of the following to convert DOS or Windows newlines to Unix newlines.

  1. This one-liner assumes that all lines end with CR+LF (carriage return + line feed)

    sed -i 's/.$//' filename 
  2. You can usually enter the ^M control char literally on the command line by first pressing Ctrl-v (control key + v key) and then Ctrl-m.  sed -i 's/^M$//' filename

  3. This following one-liner assumes that we are using GNU sed. The hex value for CR is 0x0D.

    sed -i 's/\x0D$//' filename 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.