1752676 Members
6591 Online
108789 Solutions
New Discussion юеВ

Need help

 
suchitra
Occasional Contributor

Need help

I have a problem with comparing the columns of 2 files and write a status like add, modified or delete in the outfile along with the records of these 2 file.
For ex :

consider my 1st file has say
file1
==========
p1|y|500
p2|n|500

file2
======
p1| n| 500
p3 | y|501

now my output file should be like this

output file
=========
p1 | n| 500 |c b'cas it has been changed from y to n in the 1st file
p2| n |500 | d b'cas p2 is deleted in the 2nd file
p3 | y| 501 | a b'cas p3 is added in the 2nd file

could you please help me out. I want either a shell script or a awk command .


14 REPLIES 14
Muthukumar_5
Honored Contributor

Re: Need help

Use this:

awk -F"\|" '{ var=$0;var1=$1;var2=$2;var3=$3;getline < "file2";split($0,a,"|"); if ( a[1] == var1 ) { if ( a[2] != var2 ) { print var"## c bcas "var2" is changed to "a[2];} if ( a[3] != var3 ) { print var"## c bcas "var3" is changed in 2nd file";} }
else { print var"## d bcas "var1" is deleted in 2nd file";print $0" ## a bcas "a[1]" is deleted in 2nd file";}}' file1

--
Muthu
Easy to suggest when don't know about the problem!
Steve Steel
Honored Contributor

Re: Need help

Hi

Look at the comm command

comm - select or reject lines common to two sorted files

And www.shelldorado.com


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Peter Godron
Honored Contributor

Re: Need help

Suchitra,
my ititial solution:

#!/usr/bin/sh
echo "Changed"
echo "`join -j 1 -t '|' -o 1.1 2.2 1.3 file1 file2`|c"
echo "Deleted"
cut -f1 -d '|' file1 > file1.bck
cut -f1 -d '|' file2 > file2.bck
grep `comm -23 file1.bck file2.bck` file1 > file1.res
sed "1,$ s/$/|d/" file1.res
rm file1.res
echo "Added"
grep `comm -13 file1.bck file2.bck` file2 > file2.res
sed "1,$ s/$/|a/" file2.res
rm file2.res
rm file1.bck
rm file2.bck
Peter Godron
Honored Contributor

Re: Need help

Muthukumar,
very smooth script!
The first "print var" could be replaced by:
print a[1]"|"a[2]"|"a[3]
to pick up the file2 values, rather than file1.
Senthil Kumar .A_1
Honored Contributor

Re: Need help

Hi,

I have attached a script that uses "comm" command for your situation.

Regards,
Senthil Kumar .A
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Muthukumar_5
Honored Contributor

Re: Need help

Peter,

To avoid to print a whole line as a[1]"|"a[2]"|"a[3], I have stored in a separate variable. It is help ful to simply script ;)

--
Muthu
Easy to suggest when don't know about the problem!
Peter Godron
Honored Contributor

Re: Need help

Suchitra,
something else to keep in mind is that any script using comm would only work on sorted files.

Also, are there any duplicate keys like:
p1|y|500
p1|n|300
.
.

Muthukumar_5
Honored Contributor

Re: Need help

Using perl:

#!/usr/bin/perl

open FD1,"file1" || die "Open Error: $!";
open FD2,"file2" || die "Open Error: $!";

@arr1=;
@arr2=;

for ($i=0;$i<@arr1;$i++)
{
@pat1=split (/\|/,$arr1[$i]);
@pat2=split (/\|/,$arr2[$i]);

$arr1[$i]=~chomp($arr1[$i]);
$arr2[$i]=~chomp($arr2[$i]);

if ( $pat1[0] eq $pat2[0] )
{
if ( $pat1[1] ne $pat2[1] )
{

print "$arr1[$i] | c b'cas it has been changed from $pat1[1] to $pat2[1] in field 2 in 2nd file\n";
}
if ( $pat1[2] ne $pat2[2] )
{
print "$arr1[$i] | c b'cas it has been changed from $pat1[2] to $pat2[2] in field 3 in 2nd file\n";
}

}
else
{
print "$arr1[$i] | d b'cas $pat1[0] is deleted in 2nd file\n";
print "$arr2[$i] | a b'cas $pat1[0] is added in 2nd file\n";
}

}

# END

--
Muthu
Easy to suggest when don't know about the problem!
Peter Godron
Honored Contributor

Re: Need help

Suchitra,
do these answers solve your problem?
Can you please have a look at:
http://forums1.itrc.hp.com/service/forums/helptips.do?#28
and then update the record.