compare file

file_for_compare have but directory_for_compare do not have
"file2"

file_for_compare do not have but directory_for_compare have
"file1"

can advise how to write the script ? thx in advance.

I'm afraid that paragraph explaining what you want gave me a headache :wink:

My understanding of what you are after is this:
Only look at files with a given date (eg 4th Dec)
Identify all files that are present in the 'file_to_compare' but missing in the 'directory_to_compare'
Identify all files that are present in the 'directory_to_compare' but missing in the 'file_to_compare'

Does that sound right?

If so, here's what I'd suggest:

#!/bin/sh
file_to_compare="file_to_compare"
dir_to_compare="/tmp/dir_to_compare"
filedate="$1"
dirdate="$2" # Because I'm too lazy to convert date formats
filefiles=""
dirfiles=""

#Build a list in memory to avoid making many I/O calls if the lists are long (could be ommitted if the file lists are always short)

for file in `grep $searchdate $file_to_compare | cut -d ';' -f 1 | sort`
do
  filefiles="${file} ${filefiles}"
done
for file in `ls -l $dir_to_compare | grep " ${dirdate " | awk '{ print $9 }' | sort`
do
  dirfiles="${file} ${dirfiles}"
done

filemissing=""
dirmissing=""
for file in $filefiles
do
  if echo "$dirfiles" | grep $file > /dev/null
  then
    echo "Checked $file"
  else
    echo "$file missing"
    dirmissing="${file} ${dirmissing}"
  fi
done

for file in $dirfiles
do
  if echo "$filefiles" | grep $file > /dev/null
  then
    echo "Checked $file"
  else
    echo "$file missing"
    filemissing="${file} ${filemissing}"
  fi
done

if [ -n "$dirmissing" ]
then
  echo "$file_to_compare have but $dir_to_compare do not have"
  for file in "$dirmissing"
  do
    echo "\"$file\""
  done
fi

if [ -n "$filemissing" ]
then
  echo "$file_to_compare do not have but $dir_to_compare have"
  for file in "$filemissing"
  do
    echo "\"$file\""
  done
fi

very thx for reply ,

I think I hv not clearly state what my requirement is , I hv re-write it , very thinks if you can help.

# vi file_for_compare
file1.txt;200712031200
file2.txt;200712041457
file3.txt;200712041451
file4.txt;200712051512

I have a file as above , the format is file name + date & time , I would like to compare it with the files in a directory as below

#ls /tmp/directory_for_compare
-rw-r--r-- 1 user edp 4324 Dec 04 14:57 file1
-rw-r--r-- 1 user edp 4324 Dec 04 14:57 file3
-rw-r--r-- 1 user edp 4324 Dec 05 18:57 file12

I would like to find out which file is missing for everyday ( what I want to find out is file name that the file_for_compare have but directory_for_compare don't have and vice versa ) . For example , assume today is 04-Dec , so only compare the file which the creation date is 04-Dec , in the file_for_compare , there are file2 and file3 need to compare , in the directory_for_compare , there are file1 and file3 need to compare ( because only these files creation date is 04-Dec ) , so I would like the output like below :

file_for_compare have but directory_for_compare do not have
"file2"

file_for_compare do not have but directory_for_compare have
"file1"

If today is 5-Dec , then only compare 5-Dec, if today is 6-Dec then only 6-Dec etc , no date input is required .

can advise how to write the script ? thx in advance.

You seem to be contradicting yourself, in one paragraph you are asking for a comparison for every day:

But you also say to only compare for a given day:

The script above takes the date entries on the commandline, it should be easy enough to convert it to either look at today's date, or every day. Just pick what you want and edit accordingly.

thx reply,

About the date , assume today is 4-Dec , I would like the script compare the files that are 4-Dec , if today is 5-Dec , I would like the script compre the files that are 5-Dec ( only compare today's file ) .

thx

can anyone help ? thx.

If the only problem is that it needs to look at today's date, just do a date calculation first and set the filedate and dirdate variables accordingly. It's just a matter of running the date command with the appropriate +format commandline option to produce the date formats you need. Man date for more details.

To get you started, to get something that _looks_ like the format of the files in 'file_for_compare' you can use date +%Y%m%d
The dir_for_compare one is much the same deal, just build up a string from thge %<letter> symbols and spaces.

Hi,
This one is really a difficult one. Anyway, please try my follow one, hope it is ok for you. Here i am ok

code:

echo "Please input the directory"
read dir
ls -l $dir > dir_temp
date1=`date +"%y%m%d"`
date2=`date +"%b %d"`
sed 's/;/ /g' file_for_compare > file_for_compare_temp
nawk -v d1="$date1" -v d2="$date2" '{
if (NF==2)
	if(index($2,d1)!=0)
		file[$1]=1
if (NF>2)
{
	temp=sprintf("%s %s",$6,$7)
	if(temp==d2)
		dir[$9]=1
}
}
END{
print "file_for_compare have but directory_for_compare do not have "
for (i in file)
	if(dir!=1)
		print i
print "file_for_compare do not have but directory_for_compare have"
for (j in dir)
	if(file[j]!=1)
		print j
}' file_for_compare_temp dir_temp
rm file_for_compare_temp dir_temp

Here goes:

HTH

can use:)

cmp
or
diff