Sponsored Content
Top Forums Shell Programming and Scripting Compare 2 files using sdiff command output Post 302783245 by Optimus81 on Wednesday 20th of March 2013 07:01:41 AM
Old 03-20-2013
Compare 2 files using sdiff command output

Hi All,

I have written the shell script which does the following :

a. Reads the *.cnf file content from the specified input folder path

b. Grep's some strings from the *.cnf file and writes the output in result file(*.cnf_result.txt) in output folder

c. Now, if we get new version of config files, then will put new files in input folder and run the script again

d. First script checks if there is any output files already generated, if so then it archives those files and copies to archive path

e. After archiving, it run the same grep's command onto the new file and generates the new set of result file under result path

f. Then, it check if the same file present in the archive folder if so, it compares the new result file with archived result file and generate the new *.cnf_cmp.txt file

Now, the problem is with compare result file, since am using sdiff command(does line by line comparision), it prints both old file content and as well new file content side by side
and prints the symbols ( < > | ) in between if there is any difference found.

My question is,

1. Is there any better way/solution/approach/logic that we can do this

2.Can we add color codes to the lines present in the *.cnf_cmp.txt file so that, i will be easy to read/identify the changes.

like, if there is line found in old file which is not present in new, then mark that line in yellow color.. something like that.

Thanks for reading the post anyhelp is much appreciated.
Path :
Code:
 /usr/config_check
-rwxr-xr-x 1 root root 4890 Feb 25 10:29 cc.sh
drwxr-xr-x 7 root root 7 Feb 25 11:01 input
drwxr-xr-x 6 root root 6 Feb 25 11:18 archive
-rw-r--r-- 1 root root 2940 Feb 25 11:18 list_files
drwxr-xr-x 2 root root 149 Feb 25 11:18 output

Main script :
Code:
root@atta # cat cc.sh
BASE_DIR=/usr/config_check
INPUT_DIR=$BASE_DIR/input/conf
ARCHIVE_DIR=$BASE_DIR/archive
OUTPUT_DIR=$BASE_DIR/output
COMPARE_RESULT=$OUTPUT_DIR/compare_result.txt
compare_config () {
OLD_DIR=`ls -lt $ARCHIVE_DIR | grep -v "total" | head -2 | tail -1 | awk '{print $9}'`
echo "******Compared result with files in $OLD_DIR******"
echo "****************************************************"
ls -l $ARCHIVE_DIR/$OLD_DIR | awk '{print $9}' > list_files
grep "result" list_files | while read result_file; do
#echo "Compare $OUTPUT_DIR/$result_file with $ARCHIVE_DIR/$OLD_DIR/$result_file"
if [ ! -z $OUTPUT_DIR/$result_file ]; then
sdiff $OUTPUT_DIR/$result_file $ARCHIVE_DIR/$OLD_DIR/$result_file >> $OUTPUT_DIR/`echo $result_file | sed 's/_result.txt//'`_cmp.txt
fi
done
}
echo "****************************************************"
echo "****************Running Config Checks Script********************"
[ -d $OUTPUT_DIR ] && mv $OUTPUT_DIR $ARCHIVE_DIR/Archive_$(date +%Y%m%d%H%M%S)
mkdir -p $OUTPUT_DIR
#find /usr/config_check/input/conf/ -name '*.new' -print | while read src
find /usr/config_check/input/conf/ -name '*.cnf' -print | while read src
do
dest=$(dirname "$src" | sed 's:.*/::')
dest="${dest}_$(basename "$src")"
#log=$(basename "$dest" .cnf)_result.txt
log=`echo $(basename "$dest") | sed 's/.new//'`_result.txt
cp "$src" "$OUTPUT_DIR/$dest"
echo "****Printing userName parameter result*********" >> "$OUTPUT_DIR/$log"
grep userName "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing hostList parameter result*********" >> "$OUTPUT_DIR/$log"
grep hostList "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing appServiceName parameter result*********" >> "$OUTPUT_DIR/$log"
grep appServiceName "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing routeList parameter result*********" >> "$OUTPUT_DIR/$log"
grep routeList "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing port parameter result*********" >> "$OUTPUT_DIR/$log"
grep "route\*port" "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing protocol parameter result*********" >> "$OUTPUT_DIR/$log"
grep protocol "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing serverList parameter result*********" >> "$OUTPUT_DIR/$log"
grep serviceList "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "****Printing serverId parameter result*********" >> "$OUTPUT_DIR/$log"
grep serverId "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
done
echo "**Config Checks Script execution completed.Please check the results in output folder**"
echo "****************************************************"
#Call compare config function
compare_config


Last edited by jim mcnamara; 03-20-2013 at 09:19 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

compare 2 files > output new to third

Hi, I have a question of comparing to files and output the result third file where file1 is the mainfile containing processed dir data and 2nd file grepīs dirīs data again (could be newer dirs comparing file1<file2) now i wanna make shure that output in file3 only contains newer dirs hx... (1 Reply)
Discussion started by: needle
1 Replies

2. Shell Programming and Scripting

compare files in two directories and output changed files to third directory

I have searched about 30 threads, a load of Google pages and cannot find what I am looking for. I have some of the parts but not the whole. I cannot seem to get the puzzle fit together. I have three folders, two of which contain different versions of multiple files, dist/file1.php dist/file2.php... (4 Replies)
Discussion started by: bkeep
4 Replies

3. Shell Programming and Scripting

Compare two files and get output

Hi, I have two files, file1 and file2 and I need to compare them by line (exact match, order of the lines is not important) and get output with lines from file2 that are not found in file1 (not other way around). How do I do that? With grep or otherwise.. Thankyou (2 Replies)
Discussion started by: orp56
2 Replies

4. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

5. Shell Programming and Scripting

Using sdiff without files

Hi, I'm trying to use sdiff by parsing the output of another command instead of the filename: sdiff <(echo test1) <(echo test2)However, this seems to cause my terminal session to stop working. If I use it with normal diff it works fine: ~$ diff <(echo test1) <(echo test2) 1c1 < test1... (4 Replies)
Discussion started by: Subbeh
4 Replies

6. Shell Programming and Scripting

Script to compare files recursively using sdiff

Hi All, I have been surfing to get some idea on how to compare same files from two different paths. one path will have oldfiles directory and another path will have newfiles directory. Each main directories will have sub-directories in them and each sub-directories inturn will have... (3 Replies)
Discussion started by: Optimus81
3 Replies

7. Shell Programming and Scripting

Compare the output of find command

Hi All, I am trying to run find command in a script to list out certain files based on a patter. However, when there is no file in the output, the script should exit. Tried a couple of operators (-n, -z) etc but the script does not work. I am confused whether a null string is returned... (3 Replies)
Discussion started by: danish0909
3 Replies

8. Shell Programming and Scripting

Sdiff command

Hi i am comparing file on 2 different machine with the help of script. however i am get below o/p ======= /usr/tmp ========= ======= /usr/tmp not a regular file i am not sure what does "not a regular file mean" . is it something serious, if yes then what i need to check or we can... (1 Reply)
Discussion started by: scriptor
1 Replies

9. Shell Programming and Scripting

SDiff Two files with space problem

Hello guys, I have a problem. I'm trying to use SDiff with two files which are containing spaces. My problem is that I want to save the output with > in an extra file. If I try to use it like this. sdiff "test file1" "test file2" > OutputfileI get this message: usage: diff ... (11 Replies)
Discussion started by: Mariopart
11 Replies

10. Shell Programming and Scripting

Sdiff doesn't try and compare to closest match

In the example below i would want the extensions to match. Is there any other utility or script to achieve this. Kindly help. Example: sdiff sourceFileNames targetFileNames 17021701P.blf | 17021901P.ibk 17021701P.chn | 17021901P.irk 17021701P.bmr | 17021901P.dyd 17021701P.dpf |... (7 Replies)
Discussion started by: jamilpasha
7 Replies
All times are GMT -4. The time now is 03:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy