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
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

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 06-05-2008
oabdalla oabdalla is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 2
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
  #2 (permalink)  
Old 06-05-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
You can use something like this:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'NR == FNR { 
  sub(/,/, "")
  _[$1] = $2 
  next 
  }
FNR == 1 { 
  printf "%4s %5s %8s %8s %14s\n", 
  "FS", "Total", "Used+Adj", "Free-Adj", "(Used+Adj)/Total"
  while (++i < 46) printf "-"
  print ""
  next
  }           
$1 in _ { 
  $3 += _[$1]
  $4 -= _[$1] 
  $5 = $3/$2*100 
  }
{
  printf "%4s %5d %8d %8d %14d%\n",
  $1, $2, $3, $4, $5
  }' file2.txt file1.txt
  #3 (permalink)  
Old 06-06-2008
oabdalla oabdalla is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 2
Thank you, radoulov, for your quick and elegent script. It is working perfectly.
All I need to do now is format the report nicely so it is more readable but I think I can figure that out.

Appreciate it

Omer
  #4 (permalink)  
Old 06-11-2008
vakharia Mahesh vakharia Mahesh is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 51
Question comparing the files etc

Hi Radoulov,
I have read your script .Can you eloborate for me for knowledge .Thanks.
  #5 (permalink)  
Old 06-11-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
Quote:
Originally Posted by vakharia Mahesh View Post
[...]
Can you eloborate for me for knowledge
Sure.

Code:
NR == FNR
The above expression returns true only while processing the first input file (file2). So, the corresponding actions are:

Code:
{ 
  sub(/,/, "")
  _[$1] = $2 
  next 
  }
Strip the thousands separator, populate an associative array _with $1 as keys, $2 as values, so given the input it will be:

Code:
key -> "/u01" value -> 1500
key -> "/u05" value -> 500
key -> "/u10" value -> 2500
The next statement forces awk to stop processing the current record so no further rules/actions will be executed for this record. This means that the next actions won't process the first input file.

Code:
FNR == 1
FNR is the number of records that have been read so far from the current input file (the second input file in this case). So, while processing the first record of the second input file:

Code:
{ 
  printf "%4s %5s %8s %8s %14s\n", 
  "FS", "Total", "Used+Adj", "Free-Adj", "(Used+Adj)/Total"
  while (++i < 46) printf "-"
  print ""
  next
  }
Print the header columns and go to the next record.

Code:
$1 in _ { 
  $3 += _[$1]
  $4 -= _[$1] 
  $5 = $3/$2*100 
  }
The expression key in array returns true if the indicated key exists in the indicated array. For those records do the following:
- 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
  }
Print the new values.


Hope this helps.
  #6 (permalink)  
Old 06-12-2008
vakharia Mahesh vakharia Mahesh is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 51
Thumbs up Comparing Files

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.
  #7 (permalink)  
Old 06-12-2008
vakharia Mahesh vakharia Mahesh is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 51
Thumbs up Comparing Files

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.
Closed Thread

Bookmarks

Tags
solaris

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 01:21 AM.


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