Grep multiple string from input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep multiple string from input
# 1  
Old 05-14-2014
Grep multiple string from input

How can I grep multiple strings from an input, strings are separated by space.

Strings to grep:

Code:
7680FR 6791HH 1234AA

Input:

Code:
AA 7680FR
AA 6891HH
AA 6791UA
BB 9834HA
BB 1434AB
DD 1234AA
DD 6991HH
DD 6791HH

Required output:

Code:
AA 7680FR
DD 1234AA
DD 6791HH

# 2  
Old 05-14-2014
try
Code:
grep "7680FR.*6791HH.*1234AA" filename # if you want all words
egrep -i '7680FR|6791HH|1234AA' filename # if want either of the word

filename
Code:
AA 7680FR 6791HH 1234AA
AA 6891HH
AA 6791UA
BB 9834HA
BB 1434AB
DD 1234AA
DD 6991HH
DD 6791HH

output
Code:
[Makarand] # egrep -i '7680FR|6791HH|1234AA' filename
AA 7680FR 6791HH 1234AA
DD 1234AA
DD 6791HH
[Makarand] # grep "7680FR.*6791HH.*1234AA" filename
AA 7680FR 6791HH 1234AA

# 3  
Old 05-14-2014
I have
Code:
7680FR 6791HH 1234AA

as a single variable.
# 4  
Old 05-14-2014
Try

Code:
var='7680FR 6791HH 1234AA'

Code:
grep -E "${var// /|}" file
AA 7680FR
DD 1234AA
DD 6791HH

This User Gave Thanks to clx For This Post:
# 5  
Old 05-15-2014
Quote:
Originally Posted by clx
Try

Code:
var='7680FR 6791HH 1234AA'

Code:
grep -E "${var// /|}" file
AA 7680FR
DD 1234AA
DD 6791HH

Very nice syntax.

I wonder if the string was in a file instead, would it possible to nest it inside the same sentence somehow?

so the string is in file

Code:
cat file

Ouput:

Code:
7680FR 6791HH 1234AA

I am trying to do something like,

Code:
echo ${$(cat file)// /|}

which does not work at all.
# 6  
Old 05-15-2014
Hi Kibou,
It kind of defeats the purpose if file contains the strings you want to look for and file also contains the data you're searching. The simple way to do that is:
Code:
cat file

Assuming your strings are in string_file and your data is in data_file try:
Code:
grep -E "$(tr ' ' '|' < string_file)" data_file

or:
Code:
grep -F "$(tr ' ' '\n' < string_file)" data_file

These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 05-17-2014
Quote:
Originally Posted by Don Cragun
Hi Kibou,
It kind of defeats the purpose if file contains the strings you want to look for and file also contains the data you're searching. The simple way to do that is:
Code:
cat file

Assuming your strings are in string_file and your data is in data_file try:
Code:
grep -E "$(tr ' ' '|' < string_file)" data_file

or:
Code:
grep -F "$(tr ' ' '\n' < string_file)" data_file

Thank you, Don.

I am sorry, I forgot to mention that the string and data files were different files.

I am a little perplexed with the grep -F solution. I had no idea that it could be used as an equivalent of the | operator. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Enhance existing script: Extract Multiple variables & Input in an echo string

Hi Experts I need your help to optimize my script to execute better as I have nearly 1M records & the script is taking close to 40 minutes to execute, so would need support on a faster alternative. Input: file {"house":"1024","zip":"2345","city":"asd","country":"zzv"}... (2 Replies)
Discussion started by: nk1984
2 Replies

2. UNIX for Beginners Questions & Answers

Grep for multiple string

im learning grep and i need some help. i have input word file like this fish map.this is go to.that is i want sed,awk or grep command to extract the following in this format someword SPACE someword.so output will be fish map. go to.Please use CODE tags as required by forum... (11 Replies)
Discussion started by: ahfze
11 Replies

3. Shell Programming and Scripting

Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file. I found the use of egrep "String1|String2|String3" file.txt | wc-l Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time. Can you guys help ? ... (9 Replies)
Discussion started by: nms
9 Replies

4. Shell Programming and Scripting

Multiple Results of Grep in one Line/String?

Hello, I have a Textfile sees like this "Word1":aksdfjaksdf "Word2":askdfjalsdkfdlsjfasldfj "This is Word3":asdfkjalskdfj what i need is a string which sees like this Word1;Word2;This is Word3 Conclusion always the text within "" which is before the : i tried it with grep.... (10 Replies)
Discussion started by: SwordMaster
10 Replies

5. UNIX for Dummies Questions & Answers

Multiple string input in a awk

Hi everybody, I just start my learning about Linux. So, if you can help me, it would be very good ! I have already find the possibility to find the position of a character regular expression in a line with the help of awk : test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL My script is... (2 Replies)
Discussion started by: thewizarde6
2 Replies

6. Shell Programming and Scripting

Multiple results as input to grep or awk

Hi: This is my first post in this forum. apologies if I am asking question that has been asked here multiple times. I wrote a python script that prints list of identifiers. I have another file notoriously big and with no particular format. I plan to search the identifiers in this file... (2 Replies)
Discussion started by: oriolebaltimore
2 Replies

7. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

8. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

9. UNIX for Dummies Questions & Answers

how to use grep: finding a string with double quotes and multiple digits

I have a file with a lot of lines (a lot!) that contain 10 digits between double quotes. ie "1726937489". The digits are random throughout, but always contain ten digits. I can not for the life of me, (via scouring the internet and grep how-to manuals) figure out how to find this when I search.... (3 Replies)
Discussion started by: titusbass
3 Replies

10. UNIX for Dummies Questions & Answers

Regex to match when input is not a certain string (can't use grep -v)

Hey everyone, Basically, all I'm looking for is a way to regex for not a certain string. The regex I'm looking to avoid matching is: D222 i.e. an equivalent of: awk '!/D222/' The problem is that I use this in the following command in a Bash script: ls ${source_directory} | awk... (1 Reply)
Discussion started by: kdelok
1 Replies
Login or Register to Ask a Question