Visit Our UNIX and Linux User Community


compare file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare file
# 1  
Old 12-03-2007
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.

Last edited by ust; 12-11-2007 at 12:41 PM..
# 2  
Old 12-03-2007
I'm afraid that paragraph explaining what you want gave me a headache Smilie

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:
Code:
#!/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

# 3  
Old 12-04-2007
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.
# 4  
Old 12-04-2007
You seem to be contradicting yourself, in one paragraph you are asking for a comparison for every day:
Quote:
I would like to find out which file is missing for everyday
But you also say to only compare for a given day:
Quote:
If today is 5-Dec , then only compare 5-Dec, if today is 6-Dec then only 6-Dec etc
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.
# 5  
Old 12-05-2007
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
# 6  
Old 12-11-2007
can anyone help ? thx.
# 7  
Old 12-11-2007
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #103
Difficulty: Easy
In 2019, Linux was the most popular Unix variant on the market.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

3. Shell Programming and Scripting

FASTEN count line of dat file and compare with the CTRL file

Hi All, I thinking on how to accelerate the speed on calculate the dat file against the number of records CTRL file. There are about 300 to 400 folder directories that contains both DAT and CTL files. DAT contain all the flat files records CTL is the reference check file for the... (3 Replies)
Discussion started by: ckwan
3 Replies

4. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

5. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

6. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

7. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

8. Shell Programming and Scripting

compare two files and make 1st file same as 2nd file

I am trying to compare two file and make changes where ever its different. for example: Contents of file1 IP=192.165.89.11 NM=255.255.0.0 GW=192.165.89.1 Contents of file2 IP=192.165.89.11 NM=255.255.255.255 GW=192.165.89.1 NOTE HERE THAT NM IS DIFFERENT So i want the changes... (6 Replies)
Discussion started by: pradeepreddy
6 Replies

9. Programming

compare XML/flat file with UNIX file system structure

Before i start doing something, I wanted to know whether the approach to compare XML file with UNIX file system structure. I have a pre-configured file(contains a list of paths to executables) and i need to check against the UNIX directory structure. what are the various approches should i use ? I... (6 Replies)
Discussion started by: shafi2all
6 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies

Featured Tech Videos