Compare files using Unix scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare files using Unix scripting
# 1  
Old 09-18-2012
Compare files using Unix scripting

I have a file containing the below data obtained after running a diff command

> abc 10
< abc 15
> xyz 02
<xyz 05

.....

Does anyone know how i can obtain output like :

previous value of abc is 10 and present value is 15

similarly for all the comparisons in the text file
# 2  
Old 09-18-2012
Could this help you ?

Code:
diff -b --side-by-side --suppress-common-lines file1 file2

# 3  
Old 09-18-2012
The o/p printed is the result of the diff operation .

i need to print old values and new values of abc , xyz etc using that result . how do i do that
# 4  
Old 09-18-2012
If your diff output is as simple as you posted, then try this:

Code:
awk '
    { gsub( "^.", "& " ); }   # needed if leading "<" isnt always separated by whitespace from rest of line
    /^</ {
        if( old[$2] )
            printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, old[$2], $3 );
        else
            new[$2] = $3;
    }
    /^>/ {
        if( new[$2] )
            printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, $3, new[$2] );
        else
            old[$2] = $3;
    }
'  input >output

Output should be something like:

Code:
abc  old="10"  new="15"
xyz  old="02"  new="05"

# 5  
Old 09-18-2012
What if i'm readin the diff ouput from a file ?
# 6  
Old 09-18-2012
Quote:
Originally Posted by amithpatrick1
What if i'm readin the diff ouput from a file ?
Replace 'input' on the awk command with your file name. If the file is in diff.out, then

Code:
awk '

# lines of awk programme
' diff.out

# 7  
Old 09-24-2012
I would want to redirect this output to file also .
Code:
printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, $3, new[$2] )

Currently i'm getting an error like :

Code:
awk:cmd line:11 (filename=input.txt FNR=4) fatal:division by zero

attempted when i'm trying
Code:
printf( "%s  old=\"%s\"  new=\"%s\"\n", $2, $3, new[$2] ) > output.txt

in the snippet of code given

---------- Post updated 2012-09-25 at 01:56 AM ---------- Previous update was 2012-09-24 at 01:22 PM ----------

Any solutions ??

Last edited by Franklin52; 09-24-2012 at 04:55 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files using shell scripting

Hi, I need to compare two files using shell scripting. Say like: File1 AAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCC eeeeeeeeeeeeeeeeeeeeeeeee DDDDDDDDDDDDDDDDDDDDDDDDDDDD File2 BBBBBBBBBBBBBBBBBBBBB DDDDDDDDDDDDDDDDDDDDDDDDDDDD AAAAAAAAAAAAAAAAAAAA ... (6 Replies)
Discussion started by: roshParab
6 Replies

2. Shell Programming and Scripting

Compare two files in UNIX

Hi, I have two files File1 Contents: abc dcf sdc File2 Contents: dcf sdc erg Now my program should return the contents existing in File1 but not in File2. In this case output shoud be "abc" as abc is not available in File 2. It should not return "erg" by saying it is... (4 Replies)
Discussion started by: forums123456
4 Replies

3. Shell Programming and Scripting

Compare two files in unix

Hi Gurus I need your kind help sorting the below query I have two text files File1.txt ID Name Address 101 Srinath BBB 102 Sidharth CCC File2.txt ID Name Address 102 Siddharth DDD 103 Suman EEE Now the requirement is if the second file has... (0 Replies)
Discussion started by: Pratik4891
0 Replies

4. Shell Programming and Scripting

Shell Scripting: Compare pattern in two files and merge the o/p in one.

one.txt ONS.1287677000.820.log 20Oct2010 ONS.1287677000.123.log 21Oct2010 ONS.1287677000.456.log 22Oct2010 two.txt ONS.1287677000.820.log:V AC CC EN ONS.1287677000.123.log:V AC CC EN ONS.1287677000.820.log:V AC CC EN In file two.txt i have to look for pattern which column one... (17 Replies)
Discussion started by: saluja.deepak
17 Replies

5. UNIX for Advanced & Expert Users

How to compare two files using UNIX?

I have two files which have primary key(s) for each row. I need to compare both the files and produce the output in the following format. Primary key(s),file1 value,file2 value. Both the input files will be comma separated files. I have accomplished this using perl, but it is... (6 Replies)
Discussion started by: gpsridhar
6 Replies

6. UNIX for Dummies Questions & Answers

how can i unix compare two files??

how can i unix compare two files?? var1 = 6499 7328 6351 7583 7573 var2 = 6499 7328 6351 7583 7777 i did: diff $var1 $var2 and i got the output: 1c1 < 6499 7328 6351 7583 7573 --- > 6499 7328 6351 7583 7777 what can i do with it? and what does it tell me?? how can i knoe that... (2 Replies)
Discussion started by: nirnir26
2 Replies

7. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies

8. Shell Programming and Scripting

Unix Scripting Compare Integers

I have a file with the following: 87565 82155 102656 151 162 I want to write korn shell script that will read each line in a loop and remove any number that has less than 5 digits, e.g., 151 and 152. thank you, Keoki:confused: (4 Replies)
Discussion started by: keoki_mel
4 Replies

9. UNIX for Dummies Questions & Answers

Unix Compare Files

Hi, I need to compare 2 files based on the first field in each file and output the differences to a new file. example File 1 and File 2 both have first field as Number ie: File 1 1252652355 1859553322 1778899562 File 2 1252652355 1859553322 So I would expect File 3 to... (2 Replies)
Discussion started by: Lagre1
2 Replies

10. UNIX for Advanced & Expert Users

UNIX; Compare two files

Hi Guys, Requirement: Want to compare two files, if the the content of both files is same then show "Good result" else Show "Bad Result" I am using the following logic if( cmp -s a b = 0 ) then echo "Good result" else echo "Bad result" exit 0 fi But this is... (1 Reply)
Discussion started by: abhishek3598
1 Replies
Login or Register to Ask a Question