PDA

View Full Version : awk compare files and substract



djmental
January 22nd, 2013, 04:44 PM
Hi guys,

I want to compare file1 and file2 using awk. And then using the output to file3.

file1
4
3
2

file2
5
10
1

So the output in file3 would be
1
7
1

Any idea on how to do this? I'm a newbie:oops:

Thx in advance

Lars Noodén
January 22nd, 2013, 05:26 PM
You'd have to run it through something else first to turn it into multiple columns. If there are only two files, then diff (http://manpages.ubuntu.com/manpages/precise/en/man1/diff.1posix.html) is enough.



diff -y x1 x2 | awk -F '[| \t]+' '{ A = $1 - $2; if ( A<0 ) A = -A; print A }'


The File Separator (FS) can contain a regular expression, like the one above given by -F.

djmental
January 23rd, 2013, 11:34 AM
Sorry I forgot to mention I'm using HP-UX. HP-UX doesn't seem to accept the -y parameter.

Any further options?

Lars Noodén
January 23rd, 2013, 11:51 AM
Ok. The non-standard diff came back to bite us. My awk is weak so I'm not sure of another way without escalating to perl.



#!/usr/bin/perl

use strict;
use warnings;
use integer;

my $file1 = "./file1";
my $file2 = "./file2";

open( FILE1, "$file1" ) or die ("Could not open '$file1' : $! \n");
open( FILE2, "$file2" ) or die ("Could not open '$file2' : $! \n");

while ( my $line1 = <FILE1> ) {
my $line2 = <FILE2> || last;

chomp( $line1 ); chomp( $line2 );

my $diff = abs($line1 - $line2);

print qq($diff\n);

}

close( FILE2 );
close( FILE1 );


I wish they wouldn't make non-standard extensions to core utilities. That cuts down on portability.

steeldriver
January 23rd, 2013, 12:55 PM
Here's a handy native bash 'read-from-multiple-files' that I just stole from member Vaphell :)


$$ while read -u3 a; read -u4 b; do echo $(($a-$b)); done 3< file1 4< file2
-1
-7
1
For clarity, I've omitted the absolute function

Lars Noodén
January 23rd, 2013, 01:07 PM
You both always have awesome answers.

djmental
January 23rd, 2013, 02:39 PM
Thx guys!

Gonna try this out tomorrow!