The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to print script output to screen and file orahi001 UNIX for Dummies Questions & Answers 10 01-16-2009 01:47 AM
compare columns from seven files and print the output smriti_shridhar Shell Programming and Scripting 7 06-11-2008 12:22 AM
compare two files and print the last row into first cdfd123 Shell Programming and Scripting 1 04-27-2008 08:23 AM
to compare two files and to print the difference cdfd123 Shell Programming and Scripting 5 10-06-2007 04:55 AM
Trying to compare lines in 2 files brdholman Shell Programming and Scripting 2 09-20-2007 07:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-23-2008
chlfc chlfc is offline
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
  #2 (permalink)  
Old 03-23-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

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

Regards
  #3 (permalink)  
Old 03-23-2008
chlfc chlfc is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 2
please delete reply

Last edited by chlfc; 03-24-2008 at 04:53 PM..
  #4 (permalink)  
Old 03-24-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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
Closed Thread

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 On




All times are GMT -4. The time now is 04:53 PM.


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