How to iterate Grep via all patterns provided in an input file?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to iterate Grep via all patterns provided in an input file?
# 1  
Old 07-25-2018
How to iterate Grep via all patterns provided in an input file?

When I use the following grep command with options -F and -f, its just displaying the text related to only the last pattern.

Command: $ grep -f pattern_file.txt input_file.txt

Output: doc-C2-16354

Even the following command yields the same output:
Command: $ grep -Ff pattern_file.txt input_file.txt


Contents of the pattern_file.txt:
Code:
A1
B3
C2

Contents of the input_file.txt:
Code:
doc-A1-151
file-A2-15646
table-A3-1654
file-B1-15654
doc-B2-15654
table-B3-13546
file-C1-164654
doc-C2-16354
table-C3-13565

Expected output:
Code:
doc-A1-151
table-B3-13546
 doc-C2-16354





Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!



Last edited by RudiC; 07-26-2018 at 05:47 AM.. Reason: Added CODE tags.
# 2  
Old 07-25-2018
Hello nsai,

Could you please try following and let me know if this helps you.

Code:
awk 'FNR==NR{a[$0];next} ($2 in a)' pattern_file  FS="-" input_file.txt

Thanks,
R. Singh
# 3  
Old 07-25-2018
Hello RavinderSingh,
I'm still getting the same output "doc-C2-16354"
I executed the below command:

$ awk 'FNR==NR{a[$0];next} ($2 in a)' pattern_file.txt FS="-" input_file.txt

Output: doc-C2-16354


Regards,
Sai
# 4  
Old 07-26-2018
Quote:
Originally Posted by nsai
Hello RavinderSingh,
I'm still getting the same output "doc-C2-16354"
I executed the below command:
$ awk 'FNR==NR{a[$0];next} ($2 in a)' pattern_file.txt FS="-" input_file.txt

Output: doc-C2-16354
Regards,
Sai
Hello Sai,

You could hit THANKS button for any helpful post for anyone. Coming to your question, I am getting perfect output as follows with my command see this:
Code:
awk 'FNR==NR{a[$0];next} ($2 in a)' pattern_file  FS="-" input_file.txt
doc-A1-151
table-B3-13546
doc-C2-16354

Could you please do cat -v Input_file where Input_file is your files and see if you have control M characters in them in case you have them then remove them by doing tr -d '\r < Input_file > temp_file && mv temp_file Input_file and let me know then how it goes?

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-26-2018
Yes I think at least the pattern_file is DOS format, where each search pattern have a \r at the end, but the last one, that is an incomplete line (that is completed by some versions of grep and awk; then the only the last pattern works).
Workaround: strip the trailing \r when reading from the pattern file.
Code:
awk 'FNR==NR {sub(/\r$/,""); a[$0]; next} ($2 in a)' pattern_file.txt FS="-" input_file.txt

# 6  
Old 07-26-2018
Thanks so much, RavinderSingh. Thanks for all the information. I was unaware of the Ctrl M characters earlier.
I cleaned up the ^M characters from both the pattern_file.txt and input_file.txt. Then only the output showed up.

After the cleanup in both the files, even the command $ grep -Ff "pattern_file.txt" "input_file.txt" is now giving me the expected output.

Once again, Thanks a lot for providing the valuable information on the ^M characters and the AWK & cat commands for getting my issue resolved so quickly.

Regards,
Sai
# 7  
Old 07-26-2018
Thank you, MadeInGermany. The command provided by you worked, without me meddling with the files.

Once again Thanks to you both (RavinderSingh13 and MadeInGermany) for helping me with the required commands.

------ Post updated at 09:10 AM ------

Quote:
Originally Posted by RavinderSingh13
Your Welcome nsai, you could HIT THANKS button for telling anyone thanks in this forum for their helpful posts, cheers and happy learning.

Thanks,
R. Singh
RavinderSingh,
Thanks for letting me know. I just hit the Thanks button as you mentioned. I'm new here on the forum, so in the process of getting myself familiarized with the available controls on this website.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

How to iterate a function untill last argument with any order of input?

HI I need to get the function "kick" to get executed in any way the parameters are passed in to the function. The parameters are first stored in a dictionary self.otherlist = {} print self.otherlist self.populateTestList(self.system_type) print... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

3. UNIX for Beginners Questions & Answers

Using grep to select specific patterns in text file?

How do I use grep to select words that start with I or O, end in box, and contain at least one letter in between them? the text file mailinfo.txt contains Inbox the Inbox Is a match box Doesn't match INBOX Outbox Outbox1 InbOX Ibox I box If the command works correctly it... (4 Replies)
Discussion started by: steezuschrist96
4 Replies

4. Shell Programming and Scripting

Execute function as soon as input is provided in menu drive shell script

Hi All, I have a menu driven scripts. As you know while running the script we have to input the option such as 1,2, and 3 to execute function accordingly. but after selecting the input we have to press Enter. My requirement is to execute function as soon as we press the option. Is there... (5 Replies)
Discussion started by: kiran_j
5 Replies

5. Shell Programming and Scripting

Script to select the rows from the feed file based on the input value provided

Hi Folks, I have the below feed file named abc1.txt in which you can see there is a title and below is the respective values in the rows and it is completely pipe delimited file ,. ... (3 Replies)
Discussion started by: punpun66
3 Replies

6. Shell Programming and Scripting

Using perl to grep a list of patterns from an input file

I have been struggling to grep a file of NGrams (basically clusters of consonants or Consonant and Vowel) acting as a pattern file from an Input file which contains a long list of words, one word per line. The script would do two things: Firstly read a text pattern from a large file of such... (5 Replies)
Discussion started by: gimley
5 Replies

7. Shell Programming and Scripting

Which Grep to use a file as input?

I have two files: usednaslist & naslist Using RHEL5 usednaslist >filera:/vol/EQIMS/build >filera:/vol/iquad_dev/FAST_dev naslist >server12 SunOS filera:/vol/EQIMS/build /users/uxsrvlogs >servers3 SunOS filera:/vol/iquad_dev/FAST_dev /mnt >server4 SunOS ... (2 Replies)
Discussion started by: nitrobass24
2 Replies

8. Shell Programming and Scripting

grep for certain files using a file as input to grep and then move

Hi All, I need to grep few files which has words like the below in the file name , which i want to put it in a file and and grep for the files which contain these names and move it to a new directory , full file name -C20091210.1000-20091210.1100_SMGBSC3:1000... (2 Replies)
Discussion started by: anita07
2 Replies

9. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

10. Shell Programming and Scripting

grep patterns - File

Hi I have 3 patterns for example to be searched. These three patterns are available in file1. The patterns to be searched are in file2. I want to search the pattern of file1 to file2. Can any one help with example? Regards Dhana (1 Reply)
Discussion started by: dhanamurthy
1 Replies
Login or Register to Ask a Question