Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto compare two files ?

Status
Not open for further replies.

bolobaboo

MIS
Aug 4, 2008
120
US
Hi
I have one static file and another one changes daily. We want to change static file as per daily temp file.
static file
========
$ cat tsm02_drives
DRIVE1 1110163753 no
DRIVE10 1110382438 no
DRIVE11 9110812350 no
DRIVE12 1110173780 no
DRIVE13 9110813577 no

temp file
=========
$ cat temp_tsm02_drives
DRIVE1 1110163753
DRIVE10 1110382438
DRIVE11 9110812350
DRIVE12 1110173780

Above files 1st column is drive name and 2nd clumn is drive serial number and 3rd indicates where serial number is changed.
i want to change sattic file 2nd column with temp file 2nd coumn chnages ( new serial number ) and 3rd column in static file to "yes" if that drives serial number changed.

I tried following but comparsion don't work ..
$ cat test.sh
#!/bin/ksh
rm new_tsm02_drives
while read drives serial status
do

newserial=`cat temp_tsm02_drives|grep "$drives"|awk '{print $2}'`
(( diff = $serial - $newserial ))
if [ $diff -eq 0 ]
then
echo "$drives $serial no " >> new_tsm02_drives
else
echo "$drives $newserial yes " >> new_tsm02_drives
fi
done < tsm02_drives
cp new_tsm02_drives tsm02_drives
exit 0
$
 
You may try this:
awk '
NR==FNR{s[$1]=$2;next}
{printf "%s\t%s\t%s\n",$1,s[$1],($2==s[$1]?"yes":"no")}
' temp_tsm02_drives tsm02_drives > new_tsm02_drives && mv new_tsm02_drives tsm02_drives

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi
PHV
can you explain following lines , i never understood ?
NR==FNR{s[$1]=$2;next}
{printf "%s\t%s\t%s\n",$1,s[$1],($2==s[$1]?"yes":"no")}

It will save lot of my times

Thank you again
 
Hi
PHV
Looks like comparision not working ...
$ cat tsm02_drives
DRIVE1 1110163753 no
DRIVE10 1110382438 no
DRIVE11 9110812350 no
DRIVE12 1110173780 no
$ cat temp_tsm02_drives
DRIVE1 1110163753
DRIVE10 1110382438
DRIVE11 9110812350
DRIVE12 1110173780
$cat test
#!/bin/ksh
awk '
NR==FNR{s[$1]=$2;next}
{printf "%s\t%s\t%s\n",$1,s[$1],($2==s[$1]?"yes":"no")}
' temp_tsm02_drives tsm02_drives > new_tsm02_drives && mv new_tsm02_drives tsm02_drives
exit 0
$ ./test
$ cat tsm02_drives
DRIVE1 1110163753 yes
DRIVE10 1110382438 yes
DRIVE11 9110812350 yes
DRIVE12 1110173780 yes
$
Status changed to yes though serial number are not changed.
Any idea ?
 
OOps, sorry for the typo:
awk '
NR==FNR{s[$1]=$2;next}
{printf "%s\t%s\t%s\n",$1,s[$1],($2[!]![/!]=s[$1]?"yes":"no")}
' temp_tsm02_drives tsm02_drives > new_tsm02_drives && mv new_tsm02_drives tsm02_drives

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi
PHV
Can u explain following lines ?
NR==FNR{s[$1]=$2;next}
{printf "%s\t%s\t%s\n",$1,s[$1],($2!=s[$1]?"yes":"no")}

Thank you again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top