1

I have two files below:

$cat file1
"2020051576BPI";"TS.1.BPI.20121129120000.005.txt";"Error";"'OF_USM_DBTI0000029'/'CO_SERVICE_SVCID11066'/'CO_FA_SC_600224'/. appears less times in the Input File (0), but should appear at least 1 times";""
"2019707951BPI";"TS.1.BPI.20121129120000.014.txt";"Error";"'OF_USM_DBTI0000029'/'CO_SERVICE_SVCID11066'/'CO_FA_SC_600224'/. appears less times in the Input File (0), but should appear at least 1 times";""
"Failed to parse string. Error msg: Failed to parse 'Fatal Error at file (buffer) "", line 366, column 17   Message: Expected whitespace'"

$cat file2
"'OF_USM_DBTI0000029'/'CO_SERVICE_SVCID11066'/'CO_FA_SC_600224'/. appears less times in the Input File (0), but should appear at least 1 times"
"Failed to parse string. Error msg: Failed to parse 'Fatal Error at file (buffer) "", line 366, column 17   Message: Expected whitespace'"

I am reading file2 and using grep i am finding line in file1(large file). I am doing it as below:

$cat file2|while read line
do
grep $line file1
done

But i am getting below result as below:

grep: can't open appears
grep: can't open less
grep: can't open times
grep: can't open in
grep: can't open the
grep: can't open Input
grep: can't open File
grep: can't open (0),
grep: can't open but
grep: can't open should
grep: can't open appear
grep: can't open at
grep: can't open least
grep: can't open 1
grep: can't open times"
USM_GRPSET_BPI_ERROR_20121130_171648.TXT:"2020051576BPI";"TS.1.BPI.20121129120000.005.txt";"Error";"'OF_USM_DBTI0000029'/'CO_SERVICE_SVCID11066'/'CO_FA_SC_600224'/. appears less times in the Input File (0), but should appear at least 1 times";""
USM_GRPSET_BPI_ERROR_20121130_171648.TXT:"2019707951BPI";"TS.1.BPI.20121129120000.014.txt";"Error";"'OF_USM_DBTI0000029'/'CO_SERVICE_SVCID11066'/'CO_FA_SC_600224'/. appears less times in the Input File (0), but should appear at least 1 times";""
grep: can't open to
grep: can't open parse
grep: can't open string.
grep: can't open Error
grep: can't open msg:
grep: can't open Failed
grep: can't open to
grep: can't open parse
grep: can't open 'Fatal
grep: can't open Error
grep: can't open at
grep: can't open file
grep: can't open (buffer)
grep: can't open "",
grep: can't open line
grep: can't open 366,
grep: can't open column
grep: can't open 17
grep: can't open Message:
grep: can't open Expected
grep: can't open whitespace'"
USM_GRPSET_BPI_ERROR_20121130_171648.TXT:"2019714006BPI";"TS.1.BPI.20121129120000.005.txt";"Error";"Failed to parse string. Error msg: Failed to parse 'Fatal Error at file (buffer) "", line 366, column 17   Message: Expected whitespace'";""

4 Answers 4

4

Try quoting what you are looking for:

while read line
do
  grep -F "$line" file1
done < file2
Sign up to request clarification or add additional context in comments.

3 Comments

+1. Quote all the things! You should also do grep -F -e "$line" since you're looking for exact strings rather than a regular expression (and just in case a line starts with a -). Also, the cat isn't necessary; you can just put <file2 on the line with the done.
I tried above but it showed error:grep: illegal option -- f Usage: grep -hblcnsviw pattern file . . . grep: illegal option -- f Usage: grep -hblcnsviw pattern file . . .
@sandeep7289, try a capital "F", not a lower case one.
2

You need to quote the pattern.

But, I suspect what you actually should be doing is supply the strings of file2 directly to grep thorough the -f switch:

grep -Ff file2 file1

Then any fixed string in file2 will be grepped from file1.

Note, if the lines of file2 are regular expressions you should omit the -F switch.

2 Comments

I tried above but it showed error:grep: illegal option -- f Usage: grep -hblcnsviw pattern file . . . grep: illegal option -- f Usage: grep -hblcnsviw pattern file . . .
@sandeep7289: What version of grep is this? Both -F and -f should be supported by POSIX 1003.1-2004 compliant versions of grep.
0

Add double quotes to the first parameter of grep

grep "$line" file1

OR

grep "${line}" file1

2 Comments

That's actually the first parameter, but that doesn't affect the use of quotes, which you should always use around any parameter expansion unless you intend for it to be treated as multiple shell words.
Yes. Sorry ... I will pay attention next time.
0

Use quotes and drop the useless use of cat:

while read line
do
  grep -F "$line" file1
done < file2 

2 Comments

I tried above but it showed error:grep: illegal option -- f Usage: grep -hblcnsviw pattern file . . . grep: illegal option -- f Usage: grep -hblcnsviw pattern file . . .
-F means uses fixed strings instead regexp Isn't necessary though, so if your implementation of grep doesn't have that option then just ignore it. Do grep --help to see if you have an option to use fixed strings if you're interested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.