Compare 2 files using sdiff command output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 files using sdiff command output
# 1  
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..
# 2  
Old 03-26-2013
Adding color is tricky, because the position of the gutter is dynamically determined. If you knew what is was and the escape sequences for your $TERM (which is color capable) then it is just a bit of post-processing. Maybe you should be looking for a GUI sdiff replacement, like the file comparison that came with my Borland Starteam.

Finding changes is easier if you change to a 'diff -u' format or 'diff -U99999', since the line prefix says what color to change it to. Easier to search for in text viewing tools, too.

Finding new/deleted/changed files might be faster using "comm -3 <( cd head1 ; find ... | xargs ... cksum | sort ) <( cd head2 ; find ... | xargs ... cksum | sort )|...."
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 06-22-2013
Question I need only the difference from comparision of two files.

Hi All,

Please let me know for my below query:

I have files called a.txt, b.txt. Please find the file content given below:

a.txt having:
Code:
2 123456 01 2012-01-30 0000029999
2 123548 01 2011-01-30 0000029999
2 524879 01 2002-01-30 0000030000
2 789456 01 2012-01-30 0000029999
2 123456 01 2010-05-30 0000019000

b.txt having:
Code:
2 123456 01 2012-01-30 0000029999
2 123548 01 2011-01-30 0000029999
2 524879 01 2002-01-30 0000030000

I am expecting to get the difference only as mentioned below

Code:
2 789456 01 2012-01-30 0000029999
2 123456 01 2010-05-30 0000019000

So please help me out to get the required output as mentioned through ksh command. Thanks in advance.

I am looking your reply. Please help me to solve this issue.

Thanks,
Aravindhan Devarajan

Moderator's Comments:
Mod Comment Please open a new thread for this

Last edited by Scrutinizer; 06-22-2013 at 07:14 AM.. Reason: code tags
# 4  
Old 10-16-2013
Optimus81:
Please see this link on how to add colors to output of shell scripts.
Aravindhan_D:
First, ksh is not a command per se, but a shell.
Second, this code snippet should at least get you started:
Code:
cat /dev/null > diff.txt
while read line_from_a; do
	while read line_from_b; do
		if [ "$line_from_a" != "$line_from_b" ]; then
			echo $line_from_a >> diff txt
		fi
	done < b.txt
done < a.txt

Please note that the code above makes the following assumptions:
1) The number of lines in a.txt is greater than the number of lines in b.txt.
2) To produced your desired output, all the lines in b.txt must appear in a.txt, so that only the lines that are in a.txt but not in b.txt will be redirected to diff.txt.
Hope it helps.
# 5  
Old 10-22-2013
That's a Cartesian join sort of solution. Try using associative vectors to hold one file.

If you can develop a normalized form for the files and normalize them, then any differences can be seen in context as new or missing leaves.
# 6  
Old 10-23-2013
For color message you can do something like this

Code:
$ cat color.sh
#!/bin/bash

# Display colorized information output
function cinfo() {
    COLOR='\033[01;33m'    # bold yellow
    RESET='\033[00;00m'    # normal white
    MESSAGE=${@:-"${RESET}Error: No message passed"}
    echo -e "${COLOR}${MESSAGE}${RESET}"
}
 
# Display colorized warning output
function cwarn() {
    COLOR='\033[01;31m'    # bold red
    RESET='\033[00;00m'    # normal white
    MESSAGE=${@:-"${RESET}Error: No message passed"}
    echo -e "${COLOR}${MESSAGE}${RESET}"
}


cinfo "\t\tThis is normal color yellow\n"
cwarn "\t\tThis is warning in red\n"

Resulting

Code:
$ bash color.sh
        This is normal color yellow

        This is warning in red

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question