1748213 Members
3057 Online
108759 Solutions
New Discussion юеВ

Re: Help in script

 
Waqar Razi
Regular Advisor

Help in script

I have two files. Each file has listing of directories and I want two compare what is missing in each directory. i-e Some file names are common in both lists and some are different, I just want to know what files are different in each list.

Can some one please help me in writing a script to accomplish that?
5 REPLIES 5
Mel Burslan
Honored Contributor

Re: Help in script


> /tmp/different_files.txt #clean output file


for f in `cat file1`
do
grep -q "$f" file2
r=$?
if [ $r -ne 0 ]
then
echo $f >> /tmp/different_files.txt
fi
done

for f in `cat file2`
do
grep -q "$f" file1
r=$?
if [ $r -ne 0 ]
then
echo $f >> /tmp/different_files.txt
fi
done

This is not the most elaborate way of doing what you want but most explicit way so that you can follow and understand, then if necessary, modify it. Ask if you have any questions
________________________________
UNIX because I majored in cryptology...
James R. Ferguson
Acclaimed Contributor

Re: Help in script

Hi Wagar:

If you can sort (or don't mind sorting) the two files, then you can use 'comm' to make your comparisons.

If you want to compare the contents of two directories, you can use 'diff'.

See the manpages for these commands for more information.

Regards!

...JRF...
johnsonpk
Honored Contributor

Re: Help in script

Hi Waqar,


lets assume you have two files contains file names


firstfile
---------
abc
def
fgi
abc


secondfile
---------
abc
def
ghi
abc





#!/usr/bin/sh

>present_in_second_not_in_first
>present_in_first_not_in_second
>common
cat firstfile |sort|uniq >firtfile_last
cat secondfile |sort |uniq >secondfile_last
cat firstfile_last |while read line
do
grep -x "line" secondfile_last |read dummy # you can remove -x option as it will look for exact line matching
if [ "$?" = "0" ] ;then
echo $line >>present_in_first_not_in_second
else
echo $line >>common
fi
done

cat secondfile_last |while read line
do
grep -x "line" firstfile_last |read dummy
if [ "$?" != "0" ] ; then
echo $line >>present_in_second_not_in_first
fi
done




it will give you three files
common --- common entries in both
present_in_second_not_in_first---- present in second file but not in first file

present_in_first_not_in_second---- present in first file but not in second file



Note:tested ..feel free to tell me if it is not working


Rgds
Johnson



johnsonpk
Honored Contributor

Re: Help in script

Please try this one as the previous one has typo

#!/usr/bin/sh

>present_in_second_not_in_first
>present_in_first_not_in_second
>common
cat firstfile |sort|uniq >firstfile_last
cat secondfile |sort |uniq >secondfile_last
cat firstfile_last |while read line
do
grep -x "line" secondfile_last |read dummy # you can remove -x option as it will look for exact line matching
if [ "$?" = "0" ] ;then
echo $line >>common
else
echo $line >>present_in_first_not_in_second
fi
done

cat secondfile_last |while read line
do
grep -x "line" firstfile_last |read dummy
if [ "$?" != "0" ] ; then
echo $line >>present_in_second_not_in_first
fi
done


Rgds
Johnson
Dennis Handly
Acclaimed Contributor

Re: Help in script

>Mel: This is not the most elaborate way of doing what you want but most explicit way so that you can follow and understand

Unfortunately this is the worst way performance wise, it doesn't use vector methods and is N squared. (I won't mention the evil cat and ``. ;-)

As JRF said, sort both files then use comm:
sort file1 > file1.sort
sort file2 > file2.sort
comm -3 file1.sort file2.sort

>Johnson: lets assume you have two files contains file names

Directories don't have duplicates so you don't need uniq(1). (And sort has -u.)

>present_in_second_not_in_first
>present_in_first_not_in_second
>common

To get these you simply do the following with my files above:
comm -13 file1.sort file2.sort > present_in_second_not_in_first
comm -23 file1.sort file2.sort > present_in_first_not_in_second
comm -12 file1.sort file2.sort > common