![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | 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 and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Adding columns of two files | chandra321 | Shell Programming and Scripting | 7 | 05-06-2009 12:11 PM |
| Comparing similar columns in two different files | ragavhere | Shell Programming and Scripting | 13 | 04-16-2008 08:53 AM |
| Comparing the common columns of a table in two files | ragavhere | SUN Solaris | 1 | 04-11-2008 08:41 AM |
| Comparing Columns of two FIles | ggopal | Shell Programming and Scripting | 4 | 02-21-2007 08:06 PM |
| Comparing Columns of two FIles | ggopal | UNIX for Advanced & Expert Users | 1 | 02-17-2007 04:11 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
comparing files - adding/subtracting/formating columns
I have two files:
file1.txt: FS Total Used Free Used% /u01 10000 8000 2000 80% /u02 10000 8000 2000 80% /u03 10000 8000 2000 80% /u04 10000 8000 2000 80% /u05 10000 8000 2000 80% /u06 10000 8000 2000 80% /u07 10000 8000 2000 80% /u10 10000 5000 5000 50% file2.txt: FS Adj /u01 1,500 /u05 500 /u10 2,500 I would like to compare them using the first column in each file and create an output from both that looks like the following: FS Total Used+Adj Free-Adj (Used+Adj)/Total --------- -------- --------- --------- --------- /u01 10,000 9,500 500 95% /u02 10,000 8,000 2,000 80% /u03 10,000 8,000 2,000 80% /u04 10,000 8,000 2,000 80% /u05 10,000 8,500 1,500 85% /u06 10,000 8,000 2,000 80% /u07 10,000 7,500 2,500 75% Please note that all lines from file1.txt are listed, and column "adj" of file2.txt is added to column "Used" and subtracted from column "Free" of file1.txt only if there is a match. I was able to produce this report only after loading these files into a database but I am sure I can do it using shell scripting with your help. Thanks, Omer |
|
|||||
|
Sure.
Code:
NR == FNR Code:
{
sub(/,/, "")
_[$1] = $2
next
}
Code:
key -> "/u01" value -> 1500 key -> "/u05" value -> 500 key -> "/u10" value -> 2500 Code:
FNR == 1 Code:
{
printf "%4s %5s %8s %8s %14s\n",
"FS", "Total", "Used+Adj", "Free-Adj", "(Used+Adj)/Total"
while (++i < 46) printf "-"
print ""
next
}
Code:
$1 in _ {
$3 += _[$1]
$4 -= _[$1]
$5 = $3/$2*100
}
- add the value _[$1] of the corresponding key $1 to the third column - subtract the value _[$1] of the corresponding key $1 from the fourth column - calculate the value of the fifth column Code:
{
printf "%4s %5d %8d %8d %14d%\n",
$1, $2, $3, $4, $5
}
Hope this helps. |
|
||||
|
SIMPLY SUPERB !! NO WORDSSSSSSSS WILL SUFFICE YOUR WAY OF
EXPLAINING . BUNDLES OF THANKS FROM BOTTOM OF MY HEART.Let me say to understand is somthing and to EXPLAIN in simplicity which is more cumbersome.Once again Thanx . Cheers Dimitri. |
|
||||
|
Just now I have written the reply but appers no where,
Dear Radlouv Bundles of Thanks to you for explaining the code in very simple term . No way , but SIMPLY SUPERB .No word will suffice . Knowing is something but to explain in simple wrods requires great insite . Thanx once again. Cheers Up. |
| Sponsored Links | ||
|
|