"grep -f" (patterns from file) problem

Linux howto's, compile information, information on whatever we learned on working with linux, MACOs and - of course - Products of the big evil....
Post Reply
User avatar
^rooker
Site Admin
Posts: 1481
Joined: Fri Aug 29, 2003 8:39 pm

"grep -f" (patterns from file) problem

Post by ^rooker »

[PROBLEM]
Right now I wanted to filter a file through (e)grep using a text file containing the patterns (regular expressions). When I tried each individual pattern directly on the commandline it worked flawlessly, but trying "grep -f pattern.txt bigfile.txt" returned nothing. :(

So I read the file line by line using a bash script, but when I echo'ed the read pattern things behaved weird.


[SOLUTION]
The pattern file was produced on a Windows computer, thus it was DOS encoded. Running it to "unix2dos" (or "fromdos") solved all my problems. I didn't suspect that problem at first, because usually vim would have displayed the blue ^M characters - but it didn't.


[BACKGROUND INFO]
Examples:
pattern.txt contained only 1 line (for testing):

Code: Select all

2007.10.25 12:(49|5[0-5])
The bash script was:

Code: Select all

#!/bin/bash
LOG="mylog.log"
PATTERNS="patterns.txt"

cat $PATTERNS | while read line
do
   echo $line  
   echo "left $line right"
done
The output was extremely strange:
2007.10.25 12:(49|5[0-5])
right007.10.25 12:(49|5[0-5])
...it cut the first digit off the pattern and shifted its position in the output string. Quite puzzling, but finally solved by "fromdos".
Jumping out of an airplane is not a basic instinct. Neither is breathing underwater. But put the two together and you're traveling through space!
Post Reply