Input 2 files, calculate diffs line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Input 2 files, calculate diffs line by line
# 1  
Old 06-29-2009
Input 2 files, calculate diffs line by line

Hi

I am new to Unix and need help with the following (to you all I'm sure) simple task. I am trying to output the differences between previous snaphots of various filesystem sizes to the present sizes.

I have three files (e.g.) :

file 1 file 2 file 3
10 100 /var
20 200 /opt
300 30 /tmp

I need a script to subtract the respective fields of file1 & file2, match this up with the respective fields in file3 and to output the result of this to standard output line by line.

i.e. Filesystem /var has changed by 90Kb
Filesystem /opt has changed by 180KB
Filesystem /tmp has changed by 270KB

I have tried :

#!/bin/ksh
for filesystem in `cat filesystems`
do
for curr_fs in `cat curr_fs_sizes`
do
for prev_fs in `cat prev_fs_sizes`
do
change=`expr ${curr_fs} - ${prev_fs}`
echo "Filesystem ${filesystem} has changed by ${change}Kb"
done
done
done


which cycles through every file 3 times each Smilie

Any help greatly appreciated folks ! Smilie
# 2  
Old 06-29-2009
If I have understood your requirement properly, this is what you can do...
Code:
paste file1 file2 file3 > newfile
awk '{printf("Filesystem %s has changed by %d Kb.\n",$3,$2-$1)}' newfile

# 3  
Old 06-29-2009
Not what I was expecting rakeshawasthi ! But it does of course work. Many thanks for taking the trouble to reply to me.
# 4  
Old 06-29-2009
Quote:
Originally Posted by bigbuk
...
I have three files (e.g.) :

file 1 file 2 file 3
10 100 /var
20 200 /opt
300 30 /tmp

I need a script to subtract the respective fields of file1 & file2, match this up with the respective fields in file3 and to output the result of this to standard output line by line.

i.e. Filesystem /var has changed by 90Kb
Filesystem /opt has changed by 180KB
Filesystem /tmp has changed by 270KB
...
Assuming the given files have the same number of records, you can do it in one command,

Code:
awk '{ if((getline v1 < f1)>0 && (getline v2 < f2)>0)
       print "Filesystem " $0 " has changed by " (v1>v2?v1-v2:v2-v1)"Kb"
     } ' f1="./file1" f2="./file2" file3

Output:

Code:
Filesystem /var has changed by 90Kb
Filesystem /tmp has changed by 180Kb
Filesystem /opt has changed by 270Kb

# 5  
Old 06-29-2009
yes that works too - thank you rubin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk : Want to print columns of different input files in line wise

Hi, I want to print last column of 3 input files with awk. cat file1.txt file2.txt file3.txt file1.txt 1 ad 200 2 ss 300 file2.txt 1 mm 444 2 ee 555 file3.txt 1 kk 999 2 jj 555 My o/p should be :- 1 200 444 999 2 300 555 555 (3 Replies)
Discussion started by: satishmallidi
3 Replies

3. Programming

C++ Input File line by line

Hi there, I need to read some data from a file with string and number that is similar to this: word1 0.0 1.0 0.0 word3 word4 0.0 0.0 -1.0 word1 0.0 0.0 1.0 word5With this code: #include<iostream> #include<fstream> #include<string> using namespace std; int main() ... (5 Replies)
Discussion started by: Giordano Bruno
5 Replies

4. Shell Programming and Scripting

read line by line and calculate the co-presence of variables

Hey guyz, I have a table which shows the presence or absence of my variables (A,B,C,...) in my observations (1,2,3,...) * A B C ... 1 1 0 1 2 1 1 0 3 1 0 0 ... I want to calculate the co-presence of my variables. to have a table shows the pairwise presence of the variables (have... (1 Reply)
Discussion started by: @man
1 Replies

5. Shell Programming and Scripting

Curl - input line by line from text file

Hi, I've got a text file with hundreds of lines I need to upload to an API via curl, one by one. The text file is like: 2012-08-01 10:45,124 2012-08-02 10:45,132 2012-08-03 10:45,114 I want to get curl to go through the text file sending a post for each line. like: curl --request... (0 Replies)
Discussion started by: emdeex
0 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Shell Programming and Scripting

How to calculate mean in AWK? line by line several files, thousands of lines

I'm kinda stuck on this one, I have 7 files with 30.000 lines/file like this 050 0.023 0.504336 050 0.024 0.529521 050 0.025 0.538908 050 0.026 0.537035 I want to find the mean line by line of the third column from the files named like this: Stat-f-1.dat .... Stat-f-7.dat Stat-s-1.dat... (8 Replies)
Discussion started by: AriasFco
8 Replies

8. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

9. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

10. Shell Programming and Scripting

How to prompt for input & accept input in ONE line

hi, am a new learner to shell programming. i have a script which will prompt for user to key in their name & display their name afterwards. script ===== echo "Pls enter your name:" read name echo "Your name is $name." output ===== Pls enter your name: Bob Your name is Bob. what... (2 Replies)
Discussion started by: newbie168
2 Replies
Login or Register to Ask a Question