Grep: Retrieve two strings from one file to find them anyone on line in another file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep: Retrieve two strings from one file to find them anyone on line in another file
# 1  
Old 01-19-2019
Grep: Retrieve two strings from one file to find them anywhere on line in another file

I am having trouble matching *two* strings from one file anywhere in a line of a second file, and could use some help getting this figured out. My preference would be to use grep for this because I would like to take advantage of its -A option. The latter is due to the fact that I would like both of the two strings from File1 to be found anywhere in the same line of File2, and then return the trailing n lines. A small sample from my data with desired output is as follows:

File 1:
Code:
PS001,001 HLK
PS002,004 MWQ
PS004,002 RXM
PS004,006 DBX
PS004,006 SBR

File2
Code:
 PS001,001 [VWB-WHJ <Su>] [L-GBR> <PC>]
 Lexeme     VWB HJ==     # L GBR       #
 PhraseType  2(2.1,7) 5(5,2.3)
 PhraseLab  502[0]         521[0]
 ClauseType NmCl

 PS001,001 [D-<Re>] [B->WRX> D-<WL> <Co>] [L> <Ng>] [HLK <Pr>]
 Lexeme     D      # B >WRX D <WL        # L>      # HLK      #
 PhraseType  6(6) 5(5,2.3,5,2.3) 11(11) 1(1:2)
 PhraseLab  519[0]   504[0]                510[0]    501[0]
 ClauseType xQt0

 PS001,001 [W-<Cj>] [B-R<JN> D-XVJ> <Co>] [L> <Ng>] [QM <Pr>]
 Lexeme     W      # B R<JN D XVJ        # L>      # QWM     #
 PhraseType  6(6) 5(5,2.3,5,2.3) 11(11) 1(1:2)
 PhraseLab  509[0]   504[0]                510[0]    501[0]
 ClauseType WxQ0

Desired Output:
Code:
 PS001,001 [D-<Re>] [B->WRX> D-<WL> <Co>] [L> <Ng>] [HLK <Pr>]
 Lexeme     D      # B >WRX D <WL        # L>      # HLK      #
 PhraseType  6(6) 5(5,2.3,5,2.3) 11(11) 1(1:2)
 PhraseLab  519[0]   504[0]                510[0]    501[0]
 ClauseType xQt0

I was hoping there was a way that I could use something like the following, which works great if there was only one string in File 1, rather than two.
Code:
grep -f -A4 File1 File2

I have tried various awk solutions, put this makes printing the match along with the following lines overly complicated (so it seems to me). Thanks in advance for any help you'd be willing to give.

Last edited by jvoot; 01-19-2019 at 03:22 AM.. Reason: Fixed Title
# 2  
Old 01-19-2019
Hi, try:
Code:
grep -A4 -f <(sed 's/  */.*/' File1) File2

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 01-19-2019
Such an elegant solution Scrutinizer. I had no idea grep would recognize metacharacters as a part of stdin. Thank you so very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut line from searched file if grep find neighbor columns

Hello All, While searching for the question, I found some answers but my implementation is not giving expected output. I have two files; one is sourcefile, other is named template. What I want to do is to search each line in template, when found all columns, cut the matching line from source... (4 Replies)
Discussion started by: baris35
4 Replies

2. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

3. Shell Programming and Scripting

Retrieve logs generated in last 10 mins from a log file using 'grep' command

HI All, I have a log file where the logs will be in the format as given below: 2011-05-25 02:32:51 INFO PROCESS STARTING 2011-05-25 02:32:52 INFO PROCESS STARTED . . . I want to retrieve only the logs which are less than 5 mins older than current time using grep... (3 Replies)
Discussion started by: rvhg16
3 Replies

4. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

5. Shell Programming and Scripting

grep command to retrieve one file

The Sed/Grep command is really confusing me. I know I'm missing something that should be really easy to fix. My program displays multiple names after I ask it to display only one, How do I get it to do only one?? it looks like this: Please enter a name to display? >> John (A list then... (9 Replies)
Discussion started by: toejam
9 Replies

6. UNIX for Dummies Questions & Answers

retrieve every 4th line from a file

Hello, I want to retrieve 2, 6, 10, 14...... (each 4 lines apart) from a file that looks like the sample below. In other words, I want only lines corresponding to the Xs. Header1_a XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Header1_b yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy Header2_a... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

7. Programming

to find header in Mp3 file and retrieve data

hi all, In an mp3 file , data is arranged in sequence of header and data ,how to retrieve data between two headers. Is the data between two headers fixed? because as per theory it says 1152 samples will be there , but dont knw how many bits one sample correspond to? it would help if any c... (2 Replies)
Discussion started by: shashi
2 Replies

8. UNIX for Dummies Questions & Answers

How to retrieve a particular line from a file

Hi, I want to retrieve a particular line from a file and print its content to a file. Can you suggest how to do it in UNIX ?. Rgds vinayap (2 Replies)
Discussion started by: vinayap
2 Replies

9. Shell Programming and Scripting

search and retrieve previous line in file

I've got a file with the following layout: #STMP FSgroup filename /filesysname1 filestatus 2 #STMP FSstatus filename /filesysname1 ratio 30 #STMP FSgroup filename ... (2 Replies)
Discussion started by: paulsew
2 Replies

10. Shell Programming and Scripting

how do we retrieve a line from a file in unix

we need to capture a record from a file in to a variable and do modifications to it .. so capturing line by line in a file in to some variable (2 Replies)
Discussion started by: lmadhuri
2 Replies
Login or Register to Ask a Question