Compare two files and print using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and print using awk
# 1  
Old 09-20-2012
CPU & Memory Compare two files and print using awk

I have 2 files:

email_1.out
Code:
1     abc@yahoo.com
2     abc_1@yahoo.com
3     abc_2@yahoo.com

data_1.out
Code:
<tr> 1 MAIL # 1 TO src_1 </tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">K</td> <td class="bwcol">15</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">S</td> <td class="bwcol">90</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">U</td> <td class="bcol">6</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">X</td> <td class="bcol">4</td></tr> 
<tr> 2 MAIL # 2 TO src_3 </tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">2</td> <td class="bcol">2012-09-20</td> <td class="bcol">U</td> <td class="bwcol">286</td></tr> 
<tr> 3 MAIL # 3 TO src_9 </tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">3</td> <td class="bcol">2012-09-20</td> <td class="bcol">D</td> <td class="bwcol">999</td></tr> 
<tr><td class="bcol">3</td> <td class="bcol">2012-09-20</td> <td class="bcol">K</td> <td class="bwcol">154</td></tr> 
<tr><td class="bcol">3</td> <td class="bcol">2012-09-20</td> <td class="bcol">S</td> <td class="bwcol">369</td></tr> 
<tr><td class="bcol">3</td> <td class="bcol">2012-09-20</td> <td class="bcol">U</td> <td class="bwcol">365</td></tr> 
<tr><td class="bcol">3</td> <td class="bcol">2012-09-20</td> <td class="bcol">V</td> <td class="bwcol">118</td></tr> 
<tr><td class="bcol">3</td> <td class="bcol">2012-09-20</td> <td class="bcol">X</td> <td class="bwcol">702</td></tr>

I am looking a way to separate the file and write to new file (file_1.out) based on the MAIL # and email_1.out by comparing the field 1 from email_1.out and field 5 from data_1.out

file_field 1 from email_1.out
and email should match to field 1 in email_1.out and field 5 in data_1.out
file_1.out should contain:

Code:
From:sol@yahoo.com
To: abc@yahoo.com
<html>
<head>
</head>
<body>
<table>
<tr> 1 MAIL # 1 TO src_1 </tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">K</td> <td class="bwcol">15</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">S</td> <td class="bwcol">90</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">U</td> <td class="bcol">6</td></tr> 
<tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">X</td> <td class="bcol">4</td></tr> 
</table>
</body>
</html>

Similarly
file_2.out should contain:

Code:
From:sol@yahoo.com
To: abc_1@yahoo.com
<html>
<head>
</head>
<body>
<table>
<tr> 2 MAIL # 2 TO src_3 </tr> 
<tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> 
<tr><td class="bcol">2</td> <td class="bcol">2012-09-20</td> <td class="bcol">U</td> <td class="bwcol">286</td></tr> > 
</table>
</body>
</html>


Thanks for your help.
# 2  
Old 09-21-2012
Well, your request could have been a bit clearer, e.g. where to get the "From:sol@yahoo.com" from, but based on some assumptions, try this:
Code:
awk     'function prhd () {print "From:sol@yahoo.com\nTo: "adr[$5]"\n<html>\n<head>\n</head>\n<body>\n<table>" >fname;}
         function prft () {print "<\\table>\n<\\body>\n<\html>" >fname; close(fname) }
         FNR==NR {adr[$1]=$2;next}
         /MAIL #/ {if (p++) prft(); fname="file_"$5".out"; prhd() }
         {print > fname}
         END {prft()}   
        ' email_1.out data_1.out

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print output

Hi All, i am trying to compare two files in Centos 6. F1: /tmp/d21 NAME="xvda" TYPE="disk" SIZE="40G" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="" NAME="xvda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="/boot" NAME="xvda2" TYPE="part"... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

3. Shell Programming and Scripting

Simple awk command to compare two files and print first difference

Hello, I have two text files, each with a single column, file 1: 124152970 123899868 123476854 54258288 123117283 file 2: 124152970 123899868 54258288 123117283 122108330 (5 Replies)
Discussion started by: LMHmedchem
5 Replies

4. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

5. Shell Programming and Scripting

Compare two files and print list

Hi Gents, I have 2 files as seen below. File 1: 9 1020 10 1001 11 1001 12 1002 13 1003 14 1004 15 1004 File 2: 9 1000 11 1001 12 1002 13 1003 15 1004 (5 Replies)
Discussion started by: jiam912
5 Replies

6. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

7. Shell Programming and Scripting

awk to compare flat files and print output to another file

Hello, I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me. I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3 Sample File1: ... (9 Replies)
Discussion started by: suhaeb
9 Replies

8. Shell Programming and Scripting

compare two files and print the last row into first

suppose fileA vis vis gyh gye gyh fileB vis 23 gyh 21 gye 32 output shud be like in fileA ... vis 23 vis 23 gyh 21 gye 32 gyh 21 (1 Reply)
Discussion started by: cdfd123
1 Replies

9. Shell Programming and Scripting

awk to compare lines of two files and print output on screen

hey guys, I have two files both with two columns, I have already created an awk code to ignore certain lines (e.g lines that start with 963) as they wou ld begin with a certain string, however, the rest I have added together and calculated the average. At the moment the code also displays... (3 Replies)
Discussion started by: chlfc
3 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question