awk script required to compare two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script required to compare two files
# 1  
Old 03-20-2011
awk script required to compare two files

HI friends,


I have two files.

File2 2nd column need to be compared with file1 3rd column.If ID match found, then save in different file as file1 data
followed by file2.

Code:
file1:
No;age;id;name
1;24;37h;name1
2;22;67e;name2
4;48;9r;name3
6;67;9i8;name4

file2:
exp;id;pos
23;9r;hr
15;67e;tec


Expected new file output:

No;age;id;name;exp;pos
4;28;9r;name3;23;hr
2;24;37h;name1;15;tec

Thanks in advance
vasanth
# 2  
Old 03-20-2011
Try this,
Code:
awk -F";" 'NR==FNR{a[$2]=$1FS$3;next} a[$3]{print $0 FS a[$3]}' file2 file1  > outputfile


Last edited by pravin27; 03-21-2011 at 03:00 AM..
This User Gave Thanks to pravin27 For This Post:
# 3  
Old 03-20-2011
Code:
awk -F";" 'NR==FNR{a[$2]=$1 OFS $3;next} a[$3]{print $0,a[$3]}' OFS=\; file2 file1

These 2 Users Gave Thanks to danmero For This Post:
# 4  
Old 03-20-2011
Fine..

HI,

Its worked fine. Thanks...

Can u explain the code..

Vasanth
# 5  
Old 03-20-2011
Code:
awk '
	BEGIN{
			FS=OFS=";"	# declare input Field Separator and Output Field Separator variable and set the value to ;
		}
	NR==FNR{
			a[$2]=$1 OFS $3 # store file2 information in array a indexed by key field 2 having the value of two fields(1 and 3) separated by OFS
		} 
	$3 in a{			# process the second (or subsequent) files, in this case file1, If field 3 is found as indexes in array a
			print $0,a[$3]	# print the whole record ($0) of file1 follow by the index of array a having the key field 3 of file1
		}
' file2 file1

# 6  
Old 03-21-2011
Comparison order as per the file2

Hi,

Code worked fine. Thanks for good explanation

But the order of the output file is not in the order of file2.


The code output was as per file1, i need as per file2 order.


Code:
Expected new file output:

No;age;id;name;exp;pos
4;48;9r;name3;23;hr
2;24;37h;name1;15;tec

Thanks
Vasanth
# 7  
Old 03-21-2011
Hi,
Code:
awk -F";" 'NR==FNR{a[$3]=$0;next} a[$2]{print a[$2] FS $1 FS $3}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

[awk] Compare two files

HI!! I am trying to compare two files using AWK but I have some problems. I need to count how many times letters are used in two texts. This is my script { long=length($0) for (i=1;i<=long;i++) { aux=substr($0,i,1) if ( aux != " " && aux != "" ) ... (7 Replies)
Discussion started by: ettore8888
7 Replies

3. Shell Programming and Scripting

How to use awk shell script to compare and match two files?

Basically, I have two files dupestest.txt 152,153 192,193,194 215,216 290,291 2279,2280 2282,2283haftest.txt 152,ABBOTS ROAD 153,ABBOTS ROAD 154,ABBOTS ROAD 155,ABBOTS ROAD 156,ABBOTS ROAD 157,ABBOTS ROADI want to find the numbers in dupestest.txt in haftest.txt... (4 Replies)
Discussion started by: amyc92
4 Replies

4. Shell Programming and Scripting

Compare files using awk

Please help me to compare two files and remove the items in file2 from file1 file 1:delimited using pipe(|) file1 00012|Description - 1|||||AA12345|1|AB12345|2|2012/06/03 AB123|Description - 2|||||AA12345|3|ZA11111|4|2012/06/04 11111|Description - 3|||||AP00012|1|AB12345|2|2012/06/03... (8 Replies)
Discussion started by: Mary James
8 Replies

5. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

6. Shell Programming and Scripting

awk script required

File 1 ################### TRAIU DSKDL; SLLA ;LAKJA KJAJA NSAJAN JANAL AJKJA JAJALA KAKAK JA AKA AKA AJ A A PPIN TRY1 SANT1 PPIN TRY2 SANT2 PPIN TRY3 SANT3 PPIN TRY4 SANT4 PPIN TRY5 SANT5 AJJA NA ANA ANHDJLD ALJALJA AJLJAJD LALJAL ALJALJA ALJALJA (4 Replies)
Discussion started by: jaita
4 Replies

7. UNIX for Advanced & Expert Users

Solution required for awk script

Hi Jim, The following script is in working state. But i m having one more problem with awk cmd. Could you tell me how to use any variable inside awk or how to take any variable value outside awk. My problem is i want to maintain one property file in which i am declaring variable value into that... (1 Reply)
Discussion started by: Ganesh Khandare
1 Replies

8. Shell Programming and Scripting

using awk to compare two files

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 file2: S.No ddi fi cu o/l t+ t- 1 0.8 0.9 o 0.5 0.6 2 0.5 0.2 o 0 0 3 0.2 0.3 l 0 0 4 0.5 0.6 l 0 0 (1 Reply)
Discussion started by: vasanth.vadalur
1 Replies

9. Shell Programming and Scripting

awk compare 2 files

Hi i hope some awk gurus here can help me.. here is what i need i have 2 files: File1 152445 516532 405088.pdf 152445 516533 405089.pdf 152491 516668 405153.jpg 152491 520977 408779.jpg 152491 0 409265.pdf File2 516532 /tmp/MainStreet_Sum09_Front_FNL.pdf 516533... (9 Replies)
Discussion started by: kenray
9 Replies

10. Shell Programming and Scripting

AWK script help required

Hello every one i have a very long file 'file1' like this <K>1</K> </Condition> <Tariff>Rate <Price>1.27</Price> <Interval>30</Interval> </Tariff> </Node> <Node>NonFaF <Tariff>Rate <Price>1.9</Price> <Interval>30</Interval> </Tariff> </Node> </Node> </Node> <Node>FaF (9 Replies)
Discussion started by: Dastard
9 Replies
Login or Register to Ask a Question