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
compare columns from seven files and print the output smriti_shridhar Shell Programming and Scripting 7 06-10-2008 09:22 PM
compare two files and print the last row into first cdfd123 Shell Programming and Scripting 1 04-27-2008 05:23 AM
how to print script output to screen and file orahi001 UNIX for Dummies Questions & Answers 4 04-21-2008 11:56 AM
to compare two files and to print the difference cdfd123 Shell Programming and Scripting 5 10-06-2007 01:55 AM
Trying to compare lines in 2 files brdholman Shell Programming and Scripting 2 09-20-2007 04:46 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
awk to compare lines of two files and print output on screen

hey guys, I have two files both with two columns, I have already created an
awk code to ignore certain lines (e.g lines that start with 963) as they wou
ld begin with a certain string, however, the rest I have added together and
calculated the average.

At the moment the code also displays the two files in one list showing just
the lines it used to calculate the average.

e.g
Code:
FILE1
812353124 54
812535352 55
864235235 99
963352351 35

FILE2 
812353124 75
815342325 93
864235235 52
963546253 46
***
output on screen
Code:
812353124 54
812535352 55
864235235 99
812353124 75
815342325 93
864235235 52

Average: <number calculated>
***

What I want to do now though is to compare each line on both files, and if t
he first column of a line matches one in the second file, it will print a th
ird column with that file. If one line doesn't match in the other file, the
third column will have a * on that line.

Example of output
Code:
812353124 54 75
812535352 55 *
864235235 99 52
812353124 75 *
815342325 93 *
as you can see, there are no doubles of lines that match in the first column
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-23-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,975
Post your code and we'll see if we can assist.

Regards
Reply With Quote
  #3 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
please delete reply

Last edited by chlfc; 03-24-2008 at 12:53 PM.
Reply With Quote
  #4 (permalink)  
Old 03-24-2008
Registered User
 

Join Date: Jun 2007
Posts: 379
Hi,

Follow code does not address your issue totally, but at least i think it can help you a little.

Code:
nawk 'NR==FNR {
if ($1 !~ /^963/)
{
t[$1]=$2
m[$1]=$0
}
}
NR!=FNR{
if ($1 !~ /^963/)
{
if (t[$1]!="")
t[$1]=sprintf("%s %s",$0,t[$1])
else
t[$1]=sprintf("%s *",$0)
}
}
END{
for (i in t)
if(index(t[i]," ")!=0)
	print t[i]
else
	print m[i]" *"
}' file2 file1
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:05 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