Visit Our UNIX and Linux User Community

Using regex's from file1, print line and line after matches in file2

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using regex's from file1, print line and line after matches in file2
# 1  
Old 08-02-2013
Using regex's from file1, print line and line after matches in file2

Good day,

I have a list of regular expressions in file1. For each match in file2, print the containing line and the line after.

I can match a regex and print the line and line after
awk '{lines[NR] = $0} /Macrosiphum_rosae/ {print lines [NR]; print lines [NR-1]} '

And I found code online to print lines in file2 based on matches in file1 (untested by me)
awk 'NR==FNR{a[$0]=1;next} {n=0;for(i in a){if($0~i){n=1}}} n' file1 file2

But I'm looking for help in combining the two if anyone would be so kind.

---------- Post updated at 01:50 AM ---------- Previous update was at 12:46 AM ----------


I was able to solve this.

awk 'NR==FNR{a[$0]=1;next} {n=0;for(i in a){if($0~i){n=1}}} n {print;getline;print}' file1 file2

# 2  
Old 08-02-2013
If you're using GNU grep, you can try this:
grep -A1 -f file1 file2

Previous Thread | Next Thread
Test Your Knowledge in Computers #1007
Difficulty: Medium
ix = ln(cos x+isin x)
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

2. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

3. Shell Programming and Scripting

Match single line in file1 to groups of lines in file2

I have two files. File 1 is a two-column index file, e.g. comp11084_c0_seq6:130-468(-) comp12746_c0_seq3:140-478(+) comp11084_c0_seq3:201-539(-) comp12746_c0_seq2:191-529(+) File 2 is a sequence file with headers named with the same terms that populate file 1. ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

look for line from FILE1 at FILE2

Hi guys! I'm trying to write something to find each line of file1 into file2, if line is found return YES, if not found return NO. The result can be written to a new file. Can you please help me out? FILE1 INPUT: WATER CAR SNAKE (in reality this file has about 600 lines each with a... (2 Replies)
Discussion started by: demmel
2 Replies

6. Shell Programming and Scripting

How to take out multiple matches in file2 using file1 as a reference?

Hi guys, As the title, I want to take out the lines in file2 if the TCONS_* matches that in file1. Then put the lines in a file3. The TCONS numbers ranges from TCONS_00000024 to TCONS_00381684. Those numbers are continuous in file2, not in file1. Below is just some lines of file1 and file2 ... (4 Replies)
Discussion started by: lyni2ULF
4 Replies

7. Shell Programming and Scripting

[Solved] delete line from file1 by reading from file2

Hi All, I have to arrange one of the text file by deleting specific lines. cat file1.txt 3595 3595 -0.00842773 -0.0085077 0.00368851 12815 12815 -0.00929239 0.00439785 0.0291697 3747 3747 -0.00974353 0.00228922 0.0225058 3574 3574 -0.00711399 -0.00315748 0.0141206 .... 12734... (7 Replies)
Discussion started by: senayasma
7 Replies

8. Shell Programming and Scripting

append text from file1 to the end of each line in file2

hi; my file2.txt:portname=1;list=10.11;l- portname=2;list=10.12;l- portname=3;list=10.13;l- ... my file1.txt:;"{'sector=%27'}"\&> so; i want to see:portname=1;list=10.11;l-;"{'sector=%27'}"\&> portname=2;list=10.12;l-;"{'sector=%27'}"\&> portname=3;list=10.13;l-;"{'sector=%27'}"\&>... (4 Replies)
Discussion started by: gc_sw
4 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. Shell Programming and Scripting

insert file2 after line containing patternX in file1

file1:- aaaa bbbb cccc dddd eeee file2:- 1111 2222 3333 4444 I want to insert file2 after pattern bbbb to come up with a finished file of :- aaaa bbbb 1111 2222 3333 4444 (5 Replies)
Discussion started by: repudi8or
5 Replies

Featured Tech Videos