![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 * |
| Forum Sponsor | ||
|
|
|
|||
|
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
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|