Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does grep matches all the lines no matter what the pattern

Tags:

regex

grep

bash

I'm having a problem using grep. I have a file http://pastebin.com/HxAcciCa that I want to check for certain patterns. And when I"m trying to search for it grep returns all the lines provided that the pattern already exists in the given file.

To explain more this is the code that I'm running

grep -F  "ENVIRO" "$file_pos" >> blah    

No matter what else I try even if I provide a whole line as a pattern bash always returns all the lines.
These are variations of what I'm trying:

grep -F  "E20" "$file_pos" >> blah
grep E20 "$file_pos" >> blah 
grep C:\E20-II\ENVIRO\SSNHapACS480.dll "$file_pos" >> blah
grep -F C:\E20-II\ENVIRO\SSNHapACS480.dll "$file_pos" >> blah

Also for some strange reasons when adding the -x option to grep, it doesn't return any line despite the fact that the exact pattern exists.

I've searched the web and the bash documentation for the cause but couldn't find anything.

My final test was the following

grep -F -C 1  "E20" "$store_pos" >> blah #store_pos has the same value as $file_pos

I thought maybe it was printing the lines after the result but that was not the case. I was using the blah file to see the output. Also I'm using Linux mint rebecca. Finally although the naming is quite familiar this question is not similiar to Why does grep match all lines for the pattern "\'"

And finally I would like to say that I am new to bash. I suspect The error might be due to the main file http://pastebin.com/HxAcciCa rather than the code?

like image 976
user1544624 Avatar asked May 31 '15 21:05

user1544624


People also ask

Which grep switch searches for lines that do not match the given pattern?

If you want to find all lines that do not contain a specified pattern, you can use the -v or --invert-match option.

Does grep return the whole line?

The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option. The output shows only the lines with the exact match.

What is purpose of grep and list its all options?

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out).


1 Answers

From the comments, it appears that the file has carriage returns delimiting the lines, rather than the linefeeds that grep expects; as a result, grep sees the file as one huge line, that either matches or fails to match as a whole.

(Note: there are at least three different conventions about how to delimit the lines in a "plain text" file -- unix uses newline (\n), DOS/Windows uses carriage return followed by newline (\r\n), and pre-OSX versions of MacOS used just carriage return (\r).)

I'm not clear on how your file wound up in this format, but you can fix it easily with:

tr '\r' '\n' <badfile >goodfile

or on the fly with:

tr '\r' '\n' <badfile | grep ...
like image 100
Gordon Davisson Avatar answered Oct 21 '22 10:10

Gordon Davisson