The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
grep unix.com with google



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Jun 2007
Posts: 20
Diff between 2 files using awk

Hi Experts,

Could you please help me to find the difference between two files. I tried the diff command but did not like the output as it contained < and > signs and the line numbers. Is it possible to do something using awk?

I have two files, say File1.txt contains 5 values and File2.txt contains only 2 values of what File1.txt contains. I want a command or script that will list me the 3 missing values from File2.txt. Here are the examples

File1.txt
034J
025J-01
045k
089G-02
04J01

File2.txt
025J-01
04J01

Output.txt
034J
045k
089g-02

Any help is highly appreciated.

Thanks in advance
  #2 (permalink)  
Old 11-30-2009
scottn scottn is online now Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,609
Easier with grep:


Code:
grep -vf file2 file1
034J
045k
089G-02

Or if you still want to use awk:


Code:
awk 'NR == FNR { A[$0]=1; next } !A[$0]' file2 file1
034J
045k
089G-02


Last edited by scottn; 11-30-2009 at 08:52 AM.. Reason: typo
  #3 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Nov 2009
Posts: 39
or diff... 'man diff ' , I'm pretty sure there's a way to only output differences.
EDIT: probably not so useful...even
$diff --suppress-common-lines file1 file2
still outputs the line numbers as well.

Last edited by gcampton; 11-30-2009 at 10:04 AM..
  #4 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Jun 2007
Posts: 20
Diff between 2 files using awk

Hi Scottn,

Many thanks for a quick response. I tried both you solutions.

with the grep I get the following message:

grep: illegal option -- f
usage: grep -hblcnsviw pattern file . . .

with the awk script i get
awk: syntax error near line 1
awk: bailing out near line 1

I used the commands exactly the way shown (obeviously replacing with the actual file names)

could you spot the mistake here please!!

Thanks again
  #5 (permalink)  
Old 11-30-2009
scottn scottn is online now Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,609
Ah, solaris?

Use nawk or /usr/xpg4/bin/awk.
  #6 (permalink)  
Old 11-30-2009
Registered User
 

Join Date: Oct 2009
Location: UK
Posts: 225
Two get only the entries found in file two you could use: -


Code:
diff -s <(sort file1.txt) <(sort file2.txt) | nawk ' /</{print $2}'

  #7 (permalink)  
Old 12-01-2009
Registered User
 

Join Date: Nov 2009
Posts: 22
Hi,
Any solution to this problem?
I have the same doubt

Thanks
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
diff of files giri_luck Shell Programming and Scripting 12 09-17-2009 06:02 AM
Diff b/w 2 files ecearund Shell Programming and Scripting 1 07-16-2009 02:39 AM
Diff online files nottrobin UNIX for Advanced & Expert Users 2 01-06-2009 06:04 PM
Find duplicates from multuple files with 2 diff types of files ricky007 Shell Programming and Scripting 2 03-04-2008 01:46 PM
diff 2 files; output diff's to 3rd file blt123 Shell Programming and Scripting 2 05-28-2002 12:29 PM



All times are GMT -4. The time now is 07:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0