comparing files - adding/subtracting/formating columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing files - adding/subtracting/formating columns
# 1  
Old 06-05-2008
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  
Old 06-05-2008
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  
Old 06-06-2008
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  
Old 06-11-2008
Question comparing the files etc

Hi Radoulov,
I have read your script .Can you eloborate for me for knowledge .Thanks.
# 5  
Old 06-11-2008
Quote:
Originally Posted by vakharia Mahesh
[...]
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  
Old 06-12-2008
MySQL 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  
Old 06-12-2008
MySQL 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

awk - Adding and Subtracting Numbers from 2 Columns

Hi Folks, I have a file with 2 columns TAB delimited and I want to add '1' to the first column and subtract '-1' from the second column. What I have tried so far is; awk -F"\t" '{ $1-=1;$2+=1}1' OFS='\t' file File 0623 0623 0624 0624 0643 0643 1059 1037 1037 1037 1038 1038... (2 Replies)
Discussion started by: pshields1984
2 Replies

3. Shell Programming and Scripting

Adding/ Subtracting from Date

Hi , How can I add/substruct x number of days with date? For example My_Date=`date` Now I need Hope it's clear. (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

4. Shell Programming and Scripting

Comparing two columns from two different files

Hi, I have a single-column file1 having records like: 00AB01/11 43TG22/00 78RC09/34 ...... ...... and a second file , file 2 having two columns like 78RC09/34 1 45FD11/11 2 00AB01/11 3 43TG22/00 4 ...... ...... (8 Replies)
Discussion started by: amarn
8 Replies

5. Shell Programming and Scripting

comparing two columns from two different files

Hello, I have two files as 1.txt and 2.txt with number as columns. 1.txt 0 53.7988 1 -30.0859 2 20.1632 3 14.2135 4 14.6366 5 -37.6258 . . . 31608 -8.57333 31609 -2.58554 31610 -24.2857 2.txt (1 Reply)
Discussion started by: AKD
1 Replies

6. UNIX for Dummies Questions & Answers

Comparing columns in two files

Hi, I have two files. File1.txt has 2 columns and looks like: 458739 122345 4456 122657 34200 122600 File2.txt has many columns with column 1 the same as column2 of File1.txt, but with lot more rows: 122786 abcdefg user1@email 122778 uuhjeufh user2@email... (1 Reply)
Discussion started by: ursaan
1 Replies

7. Shell Programming and Scripting

comparing 2 columns from 2 files

Hey, I have 2 files that have a name and then a number: File 1: dog 21 dog 24 cat 33 cat 27 dog 76 cat 65 File 2: dog 109 dog 248 cat 323 cat 207 cat 66 (2 Replies)
Discussion started by: dcfargo
2 Replies

8. Shell Programming and Scripting

comparing the columns in two files

I have two files file1 and file 2 both are having multiple coloumns.i want to select only two columns. i used following code to get the desired columns,with ',' as delimiter cut -d ',' -f 1,2 file1 | sort > file1.new cut -d ',' -f 1,2 file2 | sort > file2.new I want to get the coloums... (1 Reply)
Discussion started by: bab123
1 Replies

9. Shell Programming and Scripting

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (4 Replies)
Discussion started by: ggopal
4 Replies

10. UNIX for Advanced & Expert Users

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (1 Reply)
Discussion started by: ggopal
1 Replies
Login or Register to Ask a Question