Grep command in Linux in a script where the search string has a space

I have a file xyz with the following content

PPPL 0123
PPPL 0006
POFT 0923
POFT 1111
WENT 2323
SEND 2345

I also have another file named MasterFile where it contains the above mentioned data million times with different digits at the end for example some times it contains SEND 9999 or WENT 9898 or POFT 7898

Sample MasterFile

Some Data Before the Search String PPPL 0123 Some Data after the search String
Some Data Before the Search String PPPL 9999 Some Data after the search String
Some Data Before the Search String PPPL 8888 Some Data after the search String
Some Data Before the Search String PPPL 0006 Some Data after the search String
Some Data Before the Search String PPPL 7779 Some Data after the search String
Some Data Before the Search String POFT 0923 Some Data after the search String
Some Data Before the Search String POFT 9999 Some Data after the search String
Some Data Before the Search String POFT 8828 Some Data after the search String
Some Data Before the Search String POFT 1111 Some Data after the search String
Some Data Before the Search String WENT 2323 Some Data after the search String
Some Data Before the Search String WENT 9898 Some Data after the search String
Some Data Before the Search String WENT 9999 Some Data after the search String
Some Data Before the Search String SEND 2345 Some Data after the search String
Some Data Before the Search String SEND 6666 Some Data after the search String
Some Data Before the Search String SEND 7777 Some Data after the search String

I am writing my script as the following

 for i in `cat xyz`
do
grep "$i" MasterFile >> FoundString
done

even when I put the double quotes around

$i

it doesnt work Can I get some help please

Hi
Try this

grep -f MasterFile filexyz > FoundString

--- Post updated at 18:02 ---

Did I miss something? Content has changed

--- Post updated at 18:05 ---

grep -f xyz  MasterFile > FoundString

Are you saying I need to do the following? the file xyz contains the search strings that has spaces, and the MasterFile is the file that I need to grep for these search strings with spaces. and if found then save the result in FoundString

 for i in `cat xyz`
do
grep -f MasterFile "$i" >> FoundString
done

This isnt going to work

You don't need a script at all, just run this line

grep -f xyz  MasterFile > FoundString
2 Likes

Thank you so much. it worked like a charm, this is exactly what I was trying to do