LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2013, 10:02 AM   #1
bop-a-nator
LQ Newbie
 
Registered: Sep 2012
Location: North East USA
Distribution: at work: Red Hat Enterprise Linux Server release 5.8 (Tikanga); at home: what do you recommend?
Posts: 24

Rep: Reputation: Disabled
have two files to compare I want the result to be what is not matched


Hi,

this is a simple example of what I am trying to solve.

I have two files one contains a list of items (C.txt) the other contains data (B.txt) Field 4 of the B.txt file can or cannot exist in the file C.txt.

prompt> cat B.txt
Plant,hardwood,Tree,Elm,A
Plant,mixed,Tree,Maple,B
Plant,mixed,Tree,Pine,C1
Plant,softwood,Tree,Spruce,Q4
Plant,hardwood,Tree,WhiteOak,B

prompt> cat C.txt
RedPine
Spruce

I figured out out to find the entries that do exist:

/bin/gawk 'BEGIN{OFS=FS=","}NR==FNR{i[$0]; next}(($4) in i)' C.txt B.txt

Result:
Plant,mixed,Tree,RedPine,C1
Plant,softwood,Tree,Spruce,Q4


What I want to find is the opposite I want find the items that are not on the C.txt file this result:

Plant,hardwood,Tree,Elm,A
Plant,mixed,Tree,Maple,B
Plant,hardwood,Tree,WhiteOak,B

This is a simple example.

Thanks for your help,
Bop-a-nator
 
Old 01-30-2013, 10:35 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Just add a logical NOT operator:
Code:
awk 'BEGIN{OFS=FS=","}NR==FNR{i[$0]; next}!(($4) in i)' C.txt B.txt
 
1 members found this post helpful.
Old 01-30-2013, 10:53 AM   #3
bop-a-nator
LQ Newbie
 
Registered: Sep 2012
Location: North East USA
Distribution: at work: Red Hat Enterprise Linux Server release 5.8 (Tikanga); at home: what do you recommend?
Posts: 24

Original Poster
Rep: Reputation: Disabled
Smile

Ahh! So simple and does exactly what I needed. Thank you very much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
file compare and result output arn2025 Linux - Newbie 5 10-12-2012 04:12 PM
[SOLVED] Compare two fields, then sort based on result. C.L. Programming 7 09-28-2010 09:43 AM
Comparing two files to get matched contents in another file using shell script pooppp Linux - Networking 3 08-05-2008 12:11 AM
compare the hostnames in /etc/hosts with the result of nslookup on a ip from /etc/hos pete83 AIX 3 12-18-2007 03:34 AM
Compare files in specific directory to files on CD szim90 Linux - Newbie 3 02-10-2007 08:39 PM


All times are GMT -5. The time now is 07:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration