comparing files with field using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing files with field using awk
# 1  
Old 07-14-2010
comparing files with field using awk

hi,

i have 1 files a.csv temp.out

a.cvs looks like
Code:
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-209
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-219
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-220
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-221

and temp.out looks like

Code:
902,497,902,209,1003,1003,HALIFAX NS
902,497,902,219,1003,1003,HALIFAX NS
902,497,902,220,1003,1003,HALIFAX NS
902,497,902,221,1003,1003,HALIFAX NS

if 6,7,8,9 th filed of a.csv matches with 1,2,3,4 of temp.out then output should looks like

Code:
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-219,1003,1003,HALIFAX NS
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-220,1003,1003,HALIFAX NS
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902-221,1003,1003,HALIFAX NS


i am working with awk script but is unable to compare the four fileds ?

---------- Post updated at 04:04 AM ---------- Previous update was at 04:02 AM ----------

i am sorry output shold olooks like as below(that was mistake)
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902,219,1003,1003,HALIFAX NS
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902,220,1003,1003,HALIFAX NS
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902,221,1003,1003,HALIFAX NS

---------- Post updated at 04:06 AM ---------- Previous update was at 04:04 AM ----------

sorry a.csv file looks like as below

add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902,209
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902,219
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902,220
add,16390,180,674X,HALIFAX_COMMONS_X,902,497,902,221


both input and output are comma seperated
# 2  
Old 07-14-2010
Try this:
Code:
awk -F, 'NR==FNR{a[$6$7$8$9]=$0;next}
a[$1$2$3$4]{print a[$1$2$3$4], $5, $6, $7}
' OFS="," a.csv temp.out

Use nawk or /usr/xpg4/bin/awk on Solaris if you get errors.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing two files field and position

I have two files : file1 and file2 file1 format: 7026-H70 7026-1017685 7026-H70 7026-1017687 7026-B80 7026-108D65A 7026-B80 7026-108D67A 7046-B50 7026-1034B4A File2 format : mt01cp01 7026-B80 01108D69A mt01cp02 7026-B80 01108D68A mt01sv01 7046-B50... (7 Replies)
Discussion started by: amir07
7 Replies

2. Shell Programming and Scripting

awk for comparing two files

so have file1 like this: joe 123 jane 456 and then file2 like this: 123 left right 456 up down joe ding dong jane flip flop what I need to do is compare col1 and col2 in file1 with col1 in file2 and generate a new file that has lines like this: joe 123 ding dong left right jane... (11 Replies)
Discussion started by: Jaymz
11 Replies

3. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

4. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

5. Shell Programming and Scripting

comparing 2 files with awk

Hi, I'm a new user in awk and i'm trying to compare two files to create a third one if some values match in both files. The first file has this content: s 45.960746365 _21_ AGT 2490 [21:0 22:0 s 45.980418496 _21_ AGT 2491 [21:0 22:0 s 46.000090627 _21_ AGT 2492 [21:0 22:0 s 47.906552206... (2 Replies)
Discussion started by: carlosoria
2 Replies

6. UNIX for Advanced & Expert Users

Comparing two files using awk

i have one file say file1 having many records.Each record contains 2000 characters.i have to compare 192-200 (stored as name)characters in this file from other file say file2 having name stored in 1-9 characters. after comparing i have to print the record from file1 in another file say file3 ... (3 Replies)
Discussion started by: sonam273
3 Replies

7. Shell Programming and Scripting

Comparing two huge files on field basis.

Hi all, I have two large files and i want a field by field comparison for each record in it. All fields are tab seperated. file1: Email SELVAKUMAR RAMACHANDRAN Email SHILPA SAHU Web NIYATI SONI Web NIYATI SONI Email VIINII DOSHI Web RAJNISH KUMAR Web ... (4 Replies)
Discussion started by: Suman Singh
4 Replies

8. Shell Programming and Scripting

Field comparing in files

Guys trying to compare field in two files. For Ex: demo.txt 23.33.4.2 hostname 3.2.4.2 hostname12 demo1.txt 3.3.3.3 hostname23 45.23.23.23 hostname 323 I would like to compare the ips b/w these two files.any... (2 Replies)
Discussion started by: coolkid
2 Replies

9. Shell Programming and Scripting

Last field problem while comparing two csv files

Hi All, I've two .csv files as below file1.csv abc, tdf, 223, tpx jgsd, tex, 342, rpy a, jdjdsd, 423, djfkld Where as file2.csv is the new version of file1.csv with some added fields in the end of each line and some additional lines. lfj, eru, 98, jkldj, 39, jdkj9 abc, tdf, 223, tpx,... (3 Replies)
Discussion started by: ganapati
3 Replies

10. Shell Programming and Scripting

comparing two files using awk.

Hi All, a new bie to awk, How to compare substring of col1,file 1 with col2file2 and get file1contents+col3file2 as output. file1 ----- kumarfghh,23,12000,5000 rajakumar,24,14000,2500 rajeshchauhan,25,16000,2600 manoj,26,17000,2300 file 2 -------- 123,kumar,US, 123,sukumar,UK... (4 Replies)
Discussion started by: jerome Sukumar
4 Replies
Login or Register to Ask a Question