Comm compare, but column specific


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comm compare, but column specific
# 1  
Old 11-01-2011
Comm compare, but column specific

I'm looking to compare two delimited files:

file1
one|xxx
two|xxx
three|xxx

file2
four|xxx
five|xxx
six|xxx
one|yyy

Where the result is the the file2 row whose first field does NOT appear in file1. I.e., the correct result would be:

result
four|xxx
five|xxx
six|xxx

I was using this piece of awk as a starting point that does what I want except it prints the rows from file2 that DO match:

Code:
 
awk -F'|' 'NR==FNR{++a[$1];next} $1 in a'  file1 file2

I'm hoping to reverse this logic will require a minor tweak of this awk line, but I can't seem to come up with it.

Any suggestions appreciated!

Thanks,
Al
# 2  
Old 11-01-2011
Code:
awk -F'|' 'NR==FNR{++a[$1];next} ! ($1 in a)'

# 3  
Old 11-01-2011
You were right about the minor tweak Smilie
Code:
awk -F'|' 'NR==FNR{++a[$1];next} !($1 in a)' file1 file2

# 4  
Old 11-02-2011
Thanks very much folks!
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. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

3. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

4. Shell Programming and Scripting

Compare files (not using comm )

i have file1 and file2 file1 ABC XYZ file2 ABC so i used the below command comm -12 file1 file2 > matched comm -23 file1 file2 > unmatched I need some other command because this is not working in the current unix version (1 Reply)
Discussion started by: ATWC
1 Replies

5. Shell Programming and Scripting

comm command -- Sort and compare two files

Team, I have two files and I am trying to find the lines unique to file1. So i have executed the below command at shell prompt and got the correct results comm -23 <(sort test) <(sort test1) When i run the same command in Bash shell script, i got the correct results. But when i run... (5 Replies)
Discussion started by: forums123456
5 Replies

6. Shell Programming and Scripting

comm -12 based on 1 column

I'd like to eliminate the rows in two files that do not share a common value in the first column. Here's my tortured logic that is way too inefficient to consider, but might show what i'm trying to do (assume the files have been sorted): cut -f1 -d '|' file1 > file1.dat cut -f1 -d '|' file2 >... (4 Replies)
Discussion started by: tiggyboo
4 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. Shell Programming and Scripting

How do you print column 1 from comm output to a file?

Hi guys, I have a script, which after running for 20 minutes, produces a bunch of IPs. Due to a DHCP scope, some of these IPs are not useable, so I would like to eliminate them from the final list. I have used comm to do this, but am unable to extract the first column, and redirect it to a... (1 Reply)
Discussion started by: Bloke
1 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies

10. UNIX for Dummies Questions & Answers

Can you limit the compare on "comm" command

I need to find deleted records from a file. I compare yesterdays file.old to todays file.new. I need to find the records that were in yesterdays file that are not in todays. My file is fixed field. If I run a "comm -23" obviously what i find is not necesarilly a delete, it could be a change. ... (0 Replies)
Discussion started by: eja
0 Replies
Login or Register to Ask a Question