Need to compare two files

August 28, 2008 at 05:42:59
Specs: Sunos5.9, 10gb
Hi,
I have two files, file1 and file2. file2 is reference file and file1 is getting generated by sql script on fly. I need to check either all entries in file2 is present in file1 or not. If not, i need to print those entries from file2. common entries should not be there in the output file.
both the files are csv files.

file structures:

file1 looks like as below:


ORDER_IMPORT_HPIPE
ORDER_IMPORT_MHRWEB
SO_MODULE
MHE_PICKCNFM_INP
WORK_ORDER_RECPT_PROCESS
ORDER_IMPORT_ECOMMERCE

file2 looks like as below:

SO_MODULE
-
WORK_ORDER_ALLOC_PROCESS
SFA_ORDERS
CUSTOMER_IMPORT_PROCESS
WORK_ORDER_RECPT_PROCESS
ORDER_IMPORT_ECOMMERCE


output file should be

WORK_ORDER_ALLOC_PROCESS
SFA_ORDERS
CUSTOMER_IMPORT_PROCESS

kalyan


See More: Need to compare two files

Report •

#1
August 29, 2008 at 17:47:06
Brute force method. Let me know if you have any questions:


#!/bin/ksh

while read myline
do
cnt=0
while read line
do
if [[ "$myline" = "$line" ]]
then
((cnt+=1))
break
fi
done < file1
if [[ $cnt -eq 0 ]]
then
echo "$myline" >> output.file
fi
done < file2



Report •

#2
August 30, 2008 at 00:47:35
Thank you Nail for your help... It really worked..

kalyan


Report •

#3
February 24, 2010 at 08:30:41
Hey,

I am trying to do something very similar, but I have tried this, and it doesn´t work for me. I think I´m not thinking straight anymore....

attach () {
Tmensajes="$ruta_log/Tmensajes"
mensaje="$ruta_log/mensaje"
old_attach="$ruta_log/old_attachments"
send="$ruta_log/send.txt"
cd $ruta_log
if [ ! -f $Tmensajes ]
then
touch $Tmensajes && echo "Tmensajes file was created to contain a history of all the bad processes al
ready notified. " >> $LOG
fi
if [ -f $send ]
then
> $send
echo "$send has been reset for new notifications to send in an attachment by email." >> $LOG
else
touch $send && "send creado."
fi
## We now have to check which of the hanging processes have already been reported in an earlier email and rep
ort only new ones.
while read mline
do
cnt=0
while read aline
do
if [[ "$mline" = "$aline" ]]
then
((cnt+=1))
break
fi
done < $Tmensajes
if [[ $cnt -eq 0 ]]
then
echo $mline >> $send
fi
done < $mensaje
#cat $send >> $Tmensajes
}

This is part of my script. From a previous function in the same script, I have collected information about hanging processes in $mensaje file. Contents look like this:

Pid:594128 Tid:1585367 Command:/usr/local/bin/nmon_11e/nmon_aix53
Pid:766050 Tid:2777261 Command:/usr/bin/ksh
Pid:1003580 Tid:2883693 Command:/opt/IBM/SCM/client/../_jvm/jre/bin/java
Pid:630846 Tid:1822861 Command:java

I want to report on hanging processes by sending an email, but I do not want to send the same bad process twice, so I am keeping a log of sent processes in $Tmensajes. To simulate this for the purpose of the test, I have the following in Tmensaje:

Pid:594128 Tid:1585367 Command:/usr/local/bin/nmon_11e/nmon_aix53

Hence, I would like to check each line in $mensaje against the contents of $Tmensajes, and if it hasn´t been sent (it´s not in Tmensajes), then send it to another file $send for sending as an attachment. Following that, once I have the new bad processes in $send, I want to append them to $Tmensajes for the next run.

I run it as it is, and all the lines in $mensaje to to the $send file, although one of the lines is in $Tmensajes (has already been sent, so I don´t want to bombard the same information again and again).

I obviouly cannot do maintenance of the $Tmensajes file as it is either, because the $send file does not discriminate processes that have already been notified, so the $Tmensajes file would grown unnecessarily large with duplicates each time the script is run.

It is probably a silly thing, but maybe a fresh pair of eyes looking at it will spot the error in a second.

Thanks in advance!
T.


Report •
Related Solutions


Ask Question