Shell – File Compare


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell – File Compare
# 1  
Old 07-01-2011
Shell – File Compare

All,

I have a set of files in the Source , e-g
Afile.DAT
Bfile.DAT
Cfile.DAT
....
AAfile.DAT ...etc

I will know for sure the list of file name before I copy the same from the Source area
Before I copy the files to Target , I need to make sure that I have received all the files which I am looking for , even one file is missing , then I will not proceed with the further steps , need to error out and exit from the process .

Can someone please let me know the best way to handle the file compare

Thanks
-
# 2  
Old 07-01-2011
Code:
while read FILE
do
        [ -z "$FILE" ] && continue # ignore blank lines
        [ -f "/path/to/${FILE}" ] && continue

        echo "/path/to/${FILE} is missing!" >&2
        exit 1
done < filelist

# 3  
Old 07-06-2011
Thank you very much , this works great

Now I will include to check the file for any empty rows , so that I can aviod to process the same , meaning even though the file exist If has 0 rows
then I will error out those files and skip them for processing ,
I am planning to include wc -l for each file found , any advice ?

Thx
-
# 4  
Old 07-06-2011
Quote:
Originally Posted by Shanks
Now I will include to check the file for any empty rows , so that I can aviod to process the same , meaning even though the file exist If has 0 rows then I will error out those files and skip them for processing
I don't think that translated very well, come again?
# 5  
Old 07-07-2011
Oh ok , what I mean is

Let say I have 12 files , which I know for sure I should be getting for processing from the master list
Code:
Afile.DAT
Bfile.DAT
Cfile.DAT
….
AAfile.DAT …etc

Once I compare all the files from my master list , I need to make sure that each file has some rows to process , by any chance If any one of the file is empty I need to echo that file has empty rows and exit

Current code
Code:
while read FILE
do
        [ -z "$FILE" ] && continue # ignore blank lines
        [ -f "/export/home/xyz/LZ/${FILE}" ] && continue

        echo "/export/home/xyz/LZ/${FILE} is missing!" >&2
        #exit 1
done < FileList.txt

After change , ( not the actual syntax … )
Code:
while read FILE
do
        [ -z "$FILE" ] && continue # ignore blank lines
        [ -f "/export/home/xyz/LZ/${FILE}" ] && continue
        echo "/export/home/xyz/LZ/${FILE} is missing!" >&2
exit 1
chkcnt=wc -l /export/home/xyz/LZ/${FILE} | awk '{print $1}'       
if [ $chkcnt -eq 0 ] then 
            echo /export/home/xyz/LZ/${FILE} is empty 
            exit 1
fi
done < FileList.txt


Last edited by Scott; 07-07-2011 at 10:50 AM.. Reason: Use code tags, PLEASE!
# 6  
Old 07-07-2011
Oh, okay:

Code:
while read FILE
do
        [ -z "$FILE" ] && continue # ignore blank lines
        [ -s "$FILE" ] && continue # file exists and is nonzero size

        echo "/path/to/${FILE} is missing or empty!" >&2
        exit 1
done < filelist

Login or Register to Ask a Question

Previous Thread | Next Thread

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 two lines in a csv file using shell script?

I have a file lets say input.csv having two columns like- Name,Mobile No A,111 B,222 A,333 A,123 B,213 I would like to find result in a new file lets say output.csv as- Name,Mobile No A,111 B,222 means short the file on the basis of first column and first value corresponding to the... (5 Replies)
Discussion started by: Ashish Singhal
5 Replies

3. Shell Programming and Scripting

Shell script to compare and generate a new file

Requirement is I have two files their format is File1 - input_file ----- tmp_value|3|number|| tmp_value1|3|alpha|| tmp_value2|6|alpha|AA AA| tmp_value3|15|number|000000005| tmp_value4|15|number|000000000000000| tmp_value5|11|alpha|bbbbbbbbbbb| tmp_value6|11|alpha|bb bb| input_file ... (4 Replies)
Discussion started by: greenworld123
4 Replies

4. UNIX for Dummies Questions & Answers

if statements in shell to compare value

Hi, I am a newbie to unix, I am trying to write a shell script (just a test) but I can't seem to compare the variable value with a string in the if statement. I get an error when i execute the following script, could you tell me what is wrong and how I can correct it Note - I am using bash... (4 Replies)
Discussion started by: muthuveerappan
4 Replies

5. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

6. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

7. Shell Programming and Scripting

help to compare data from file using shell script to c++ program

hello ....i am new in shell script..I have one shell script which read inputs from file...and second is c++ pgm..i call my script from c++ programm..to compare file values to c++ pgm value.. our shell script is shubhi.sh #! /bin/sh while read line do echo -e"$line" done<file1 this script... (1 Reply)
Discussion started by: shubhig15
1 Replies

8. Shell Programming and Scripting

shell script to compare file contents

Hello Has anyone got an example shell script that I can use to compare the contents of two files. The files should contain the same contents, eg. file1.txt apple pear grape file2.txt apple pear grape (2 Replies)
Discussion started by: deedaz
2 Replies

9. Shell Programming and Scripting

Compare one file with 7 files in shell programming

hi , I am new to shell. i have a requirement to compare the one file from one dir to other 7 files of other dir and to find out the duplicate files from the 7 files . the name of the all files go on changing every weekend. Thanks in advance (4 Replies)
Discussion started by: ravi214u
4 Replies

10. UNIX for Advanced & Expert Users

how to compare text value in shell

Hi, I need to check that if user is not oracle the the script should exit out . But the below script always exits evenif I am logged as oracle. Plz help me........... USER=`id` echo $USER if ; then echo " You are logged as `id` , it must be oracle " exit 2 fi Regards (2 Replies)
Discussion started by: reply2soumya
2 Replies
Login or Register to Ask a Question