awk file comparison, x lines after matching as output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk file comparison, x lines after matching as output
# 1  
Old 01-22-2012
awk file comparison, x lines after matching as output

Hello,

I couldn't find anything on the Forum that would help me to solve this problem. Could any body help me process below data using awk?
I have got two files:
file1:
Code:
Worker1:    Thomas
Position:    Manager
Department:    Sales       
Salary:        $5,000
Worker2:    Jason
Position:    Developer  
Department:    Technology  
Salary:        $5,500
Worker3:    Sanjay
Position:    Sysadmin   
Department:    Technology  
Salary:        $7,000
Worker4:    Nisha
Position:    Manager    
Department:    Marketing   
Salary:        $9,500
Worker5:    Randy
Position:    DBA        
Department:    Technology  
Salary:        $6,000

file2:
Code:
Thomas
Nisha

I need to create file3 that contains x lines from file2 after occurrence of expression from file1 to look like
Code:
Worker1:    Thomas
Position:    Manager
Department:    Sales       
Salary:        $5,000
Worker4:    Nisha
Position:    Manager    
Department:    Marketing   
Salary:        $9,500

# 2  
Old 01-22-2012
Try:
Code:
awk 'NR==FNR{A[$1]=1;next}A[$2]{print; for (i=1;i<=3;i++){getline;print}}'  file2 file1

-or-
Code:
awk 'NR==FNR{A[$1]=1;next}A[$2]{n=4}n-->0' file2 file1

# 3  
Old 01-22-2012
Code:
awk 'NR==FNR{a[$0]=1;next} a[$2]{p=4} p-->0' file2 file1

--ahamed
# 4  
Old 01-22-2012
GOD BLESS YOU!! Thaks
# 5  
Old 01-22-2012
Or if your grep supports it:
Code:
grep -A3 -f file2 file1

# 6  
Old 01-22-2012
Quote:
Originally Posted by Scrutinizer
Or if your grep supports it:
Code:
grep -A3 -f file2 file1

Yes, but it is very slow because of the file size.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to average matching lines in file

The awk below executes and is close (producing the first 4 columns in desired). However, when I add the sum of $7, I get nothing returned. Basically, I am trying to combine all the matching $4 in f1 and output them with the average of $7 in each match. Thank you :). f1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to add lines with symbol to output file

In the awk below which does execute I get output that is close, except for all the lines that start with a # are removed. Some lines have one others two or three and after the script adds the ID= to the fields below the pattern in the awk, I can not seem to add the # lines back to the output. ... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Help With AWK Matching and Re-printing Lines

Hi All, I'm looking to use AWK to pattern match lines in XML file - Example patten for below sample would be /^<apple>/ The sample I wrote out is very basic compared to what I am actually working with but it will get me started I would like to keep the matched line(s) unchanged but have them... (4 Replies)
Discussion started by: rhoderidge
4 Replies

5. UNIX for Dummies Questions & Answers

df -> output files; comparison using awk or...

:wall: I am trying to do the following using awk (is that the best way?): Read 2 files created from the output of df (say, on different days) and compare the entries using the 1st (FileSys) and 6th (Mount) fields to see if the size has changed. Output (at least), to a new file (some header... (2 Replies)
Discussion started by: renata
2 Replies

6. Shell Programming and Scripting

Finding lines matching the Pattern and their previous lines in a file

Hi, I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using: grep -w 'Possible network disconnect' file_name. But I am more interested in getting the timing of these events which are... (7 Replies)
Discussion started by: sagarparadkar
7 Replies

7. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

8. Shell Programming and Scripting

Merge lines in a file with Awk - incorrect output

Hi, I would like: FastEthernet0/0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored 0 output errors, 0 collisions, 0 interface resets Serial1/0:0 is up, line protocol is up 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort 0... (14 Replies)
Discussion started by: mv652
14 Replies

9. Shell Programming and Scripting

awk should output if one input file doesnt have matching key

nawk -F, 'FNR==NR{a= $3 ;next} $2 in a{print $1, 'Person',$2, a}' OFS=, filea fileb Input filea Input fileb output i am getting : (2 Replies)
Discussion started by: pinnacle
2 Replies

10. Shell Programming and Scripting

Output format - comparison with I/p file

Hi, I have a file which contains more than 1 lakh records like following: a. name, id, city, state, country, phone (Expected I/P file format) name, id, city,, state, country, phone (Current I/P file format ) I want to achieve following tasks, a, Remove the extra comma in the... (1 Reply)
Discussion started by: velappangs
1 Replies
Login or Register to Ask a Question