The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparing two files ragavhere Shell Programming and Scripting 31 06-12-2008 01:12 AM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 12:34 AM
Comparing two files.. padarthy Shell Programming and Scripting 1 08-29-2007 05:01 AM
Comparing two files... paqman Shell Programming and Scripting 12 08-08-2007 12:45 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-08-2007 10:38 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-05-2005
Registered User
 

Join Date: Jan 2002
Posts: 60
awk - comparing files

I've been trying to use awk to compare two files that have pretty much the same data in apart from certain lines where in one file a fields value has changed. I want to print the line from the first file and the changed line from the second file.
At the moment, all I can get it to do is print the line that contains the changed value.
Here's some example data:

File1:
111:222:333:444
222:333:444:555
333:444:555:666

File2:
112:222:333:444
222:333:444:555
333:444:555:667

As you can see the first field in the first line in file2 has changed and also the last field in last line has changed. So I'm looking for the output to be something like:

file1 old - 111:222:333:444
file2 new - 112:222:333:444
---------------------------
file1 old - 333:444:555:666
file2 new - 333:444:555:667

The awk I've written so far is:

awk 'BEGIN {
while (getline < "file1" > 0)
arr[$0] = $0
}
{
if ($0 in arr) {
continue
} else {
print NR" - file2 - "$0
print "-----------------------------------------------------------------------------------------------"
}
}
' file2

This only prints the line that has changed in the second file

Can any one help?

Cheers
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-05-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
nawk -f db.awk file1 file2

here's db.awk:
Code:
BEGIN {
  FS=":"
}

FNR == NR { if (FNR==1) file1=FILENAME;  arr[NR]=$0; next }
{
  if ($0 != arr[FNR]) {
    printf("%s old - %s\n", file1, arr[FNR])
    printf("%s new - %s\n", FILENAME, $0)
    print "-----------------------------"
  }
}
Reply With Quote
  #3 (permalink)  
Old 05-06-2005
Registered User
 

Join Date: Jan 2002
Posts: 60
Thank you kind sir...that's exactly what I'm looking for

Cheers
Reply With Quote
  #4 (permalink)  
Old 08-22-2008
Registered User
 

Join Date: Aug 2008
Posts: 8
Comparing two files and getting the output

Quote:
Originally Posted by vgersh99 View Post
nawk -f db.awk file1 file2

here's db.awk:
Code:
BEGIN {
  FS=":"
}
 
FNR == NR { if (FNR==1) file1=FILENAME;  arr[NR]=$0; next }
{
  if ($0 != arr[FNR]) {
    printf("%s old - %s\n", file1, arr[FNR])
    printf("%s new - %s\n", FILENAME, $0)
    print "-----------------------------"
  }
}
Hi,
I have 2 files File1.txt and File2.txt

File1.txt
123|Rosy|Chicago|
234|stella|michigan|
999|Richard|NJ|

File2.txt
123|Rosy|Chicago|
235|Stella|michigan|
999|Richard|NJ|ABN

I want to compare the two files and get the output as below:
Old:234|stella|michigan
New:235|Stella|michigan|

I used your above solution,but its giving error.

I used the command awk -f db.awk File1.txt File2.txt

Can someone help me in this regard?
Reply With Quote
  #5 (permalink)  
Old 08-22-2008
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Pls don't hijack threads - create a new thread for your issue.
Read the rules.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0