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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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:

Code:
AAA,Apples,123
BBB,Bananas,124
CCC,Carrot,125

file2.txt:

Code:
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 like below:

Code:
Store4|DDD|126|38

i.e. after comparing the field2 of file2.txt with field1 of file1.txt, if there is any entry missing in file1.txt, that needs to be returned on screen

Do you have any easy approach to achieve this with an one liner using awk?

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 09-24-2011 at 06:38 AM..
# 2  
Code:
awk -F"[,|]" 'NR==FNR{a[$1]++;next} !a[$2]' file1 file2

--ahamed
# 3  
Hi ahamed101....thanks a lot....your solution works fine....but,if I want to return only the missing field for e.g. DDD in above case, how to modify this command? I have redirected the output to a file and then applied sort/unique commands alternatively. If it is possible to add that logic in your command, please let me know.....thanks again
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #140
Difficulty: Easy
Unix and the C programming language were both developed at AT&T.
True or False?

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

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

3. UNIX for Dummies Questions & Answers

Can one use 2 field separators in awk?

I have files such as n02-z30-dsr65-terr0.25-dc0.008-16x12drw-run1.cmd I am wondering if it is possible to define two field separators "-" and "." for these strings so that $7 is run1. (5 Replies)
Discussion started by: kristinu
5 Replies

4. UNIX Desktop Questions & Answers

awk Varing Field Separators

Hi Guys, I have small dilemma which I could do with a little help solving . I currently have text HDD S.M.A.R.T report which I have pasted below: smartctl 5.39 2008-10-24 22:33 (openSUSE RPM) Copyright (C) 2002-8 by Bruce Allen, http://smartmontools.sourceforge.net Device: COMPAQ... (2 Replies)
Discussion started by: bikerben
2 Replies

5. Shell Programming and Scripting

comparing files with field using awk

hi, i have 1 files a.csv temp.out a.cvs looks like 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... (1 Reply)
Discussion started by: raghavendra.cse
1 Replies

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

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

8. Shell Programming and Scripting

Multiple input field Separators in awk.

I saw a couple of posts here referencing how to handle more than one input field separator in awk. I figured I would share how I (just!) figured out how to turn this line in a logfile: 90000000000000000000010001 name... (4 Replies)
Discussion started by: kinksville
4 Replies

9. Shell Programming and Scripting

Awk Multiple Field Separators

Hi Guys, I'm tying to split a line similar to this: YO6-2000-30.htm: (3 properties found). Into seperate columns, so effectively I need to check for a - . : tab and space in the statement. Any help would be appreciated Thanks! (7 Replies)
Discussion started by: Tonka52
7 Replies

10. UNIX for Dummies Questions & Answers

Comparing two files and noting matches

I have two files. One contains names and another file (66 MB, ASCII format) contains details of persons. How do I compare the names in the first file with the second file and write the matches to a third file. I would prefer this to be solved in UNIX or VB. Thanks. (2 Replies)
Discussion started by: augustinep
2 Replies

Featured Tech Videos