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


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep command in Linux in a script where the search string has a space
# 1  
Old 12-12-2019
Grep command in Linux in a script where the search string has a space

I have a file xyz with the following content
Code:
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
Code:
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
Code:
 for i in `cat xyz`
do
grep "$i" MasterFile >> FoundString
done

even when I put the double quotes around
Code:
$i

it doesnt work Can I get some help please
# 2  
Old 12-12-2019
Hi
Try this
Code:
grep -f MasterFile filexyz > FoundString

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

Did I miss something? Content has changed

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

Code:
grep -f xyz  MasterFile > FoundString

# 3  
Old 12-12-2019
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

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

This isnt going to work
# 4  
Old 12-12-2019
You don't need a script at all, just run this line
Code:
grep -f xyz  MasterFile > FoundString

These 2 Users Gave Thanks to nezabudka For This Post:
# 5  
Old 12-12-2019
Thank you so much. it worked like a charm, this is exactly what I was trying to do
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Linux shell script, search by an input string

So, there is a large file where I have to conduct several search using bash shell scripting. The file is like this: TITLE and AUTHOR ETEXT NO. Aspects of plant life; with special reference to the British flora, 56900 by Robert Lloyd... (1 Reply)
Discussion started by: Philia
1 Replies

3. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

Grep line with all string in the lines and not space.

I want to write the syntax so does not count line with no space. So currerntly it is showing lines as 5, but i want to show 4. # cat /tmp/mediacheck | sort -u | grep -vi " " | awk '{print $1}' | wc -l BA7552 BAA002 BAA003 BAA004 (6 Replies)
Discussion started by: Junes
6 Replies

6. Shell Programming and Scripting

grep for a string until instance of a space

Hey guys, I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this. I'm looking to grep for the string ERROR from the following log up until any... (6 Replies)
Discussion started by: terrell
6 Replies

7. UNIX for Dummies Questions & Answers

Matching exact string with blank space using grep

hi! i'm trying to get grep to do an exact match for the following pattern but..it's not quite working. I'm not too sure where did I get it wrong. any input is appreciated. echo "$VAR" | grep -q '^test:]name' if ; then printf "test name is not found \n" fi on... (4 Replies)
Discussion started by: jazzaddict
4 Replies

8. AIX

grep not working when search string has a space in it

Hi, I am trying to grep a string which has two words separated by space. I used a script to grep the string by reading the string in to a variable command i used in the script is echo "enter your string" read str grep $str <file> it is working fine when the entered string is a single... (3 Replies)
Discussion started by: sekhar gajjala
3 Replies

9. Shell Programming and Scripting

grep : search a long char contain space

Hi, i have to search for a char like that : export var1="i am not happy /not happy" with a command like : grep $var1 file but this not working with me !!! thank you in advance. (2 Replies)
Discussion started by: tizilfin
2 Replies

10. UNIX for Dummies Questions & Answers

grep for a search string

Hi, I want to grep one file for a search string and get the next 10 lines after the search string. for eg,in file tnsnames.ora,i have 100 entries.i want to search for one string and get the entry for that db. please help how to acheive this using awk or sed? Thanks. (11 Replies)
Discussion started by: raga
11 Replies
Login or Register to Ask a Question