Comparing files columnwise and print the differences in third file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing files columnwise and print the differences in third file
# 1  
Old 03-17-2009
Bug Comparing files columnwise and print the differences in third file

Hello Everybody!!!!!!!!! Request you to help me with the below mentioned issue:

I have 2 files say,

File 1:
a|4|7
b|3|2
c|8|8
d|8|9

File 2:
a|4|6
b|2|2
c|8|8
d|9|8

The third file(output file) should have:
Data mismatch in row 1 column 3
Data mismatch in row 2 coumn 2
Data mismatch in row 4 column 2 and column 3

Or atleast be able to capture the row and column number having mismatch.....

Please let me know if it is possible
# 2  
Old 03-17-2009
Hello there,

I think the following KornShell script will do the job. Note that this script works if and only if we suppose that in each file there are 4 lines and 3 columns. Otherwise a modification is needed in order to have it working for an arbitrary number of lines and columns.

Code:
#!/bin/ksh

INPUT_FILE1=$1
INPUT_FILE2=$2
OUTPUT_FILE=$3

exec 4< $INPUT_FILE1
exec 5< $INPUT_FILE2
exec 6> $OUTPUT_FILE

    
for LINE in 1 2 3 4
do
    read -u4 CURRENT_LINE_IN_FILE1
    read -u5 CURRENT_LINE_IN_FILE2
    
    for COLUMN in 1 2 3
    do
        TOKEN1=$(echo $CURRENT_LINE_IN_FILE1 | cut -d '|' -f $COLUMN)
        TOKEN2=$(echo $CURRENT_LINE_IN_FILE2 | cut -d '|' -f $COLUMN)
        
        if (( (($LINE == 1) && ($COLUMN == 3)) ||
              (($LINE == 2) && ($COLUMN == 2)) ||
              (($LINE == 4) && (($COLUMN == 2) || ($COLUMN == 3))) ))
        then
            if [[ $TOKEN1 != $TOKEN2 ]]
            then
                print -u6 "Mismatch found in (line , column) = "\
                "($LINE , $COLUMN), in file: $INPUT_FILE1 found "\
                "$TOKEN1 but in file: $INPUT_FILE2 found $TOKEN2"
            fi
        fi
    done
done

4<&-
5<&-
6<&-

Regards,
Smilie
# 3  
Old 03-17-2009
Bug

Thanks a lot....actually the no. of columns are static(i.e. can be greater than 3) while number of rows are dyanamic.Also I am currently working on csh shell.
Please let me know if it can be done to satisfy the above requirements.
Also let me know if we can do it using any command like diff,cmp,comm or anything else.
# 4  
Old 03-18-2009
Please let me know if it is at all possible.......I have tried almost everything except awk script.But not able to arrive at a solution.Please suggest
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading Xml file and print the values into the text file in columnwise?

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (4 Replies)
Discussion started by: sravanreddy
4 Replies

2. Shell Programming and Scripting

Comparing 2 xml files and print the differences only in output

Hi....I'm having 2 xml files, one is having some special characters and another is a clean xml file does not have any special characters. Now I need one audit kind of file which will show me only from which line the special characters have been removed and the special characters. Can you please... (1 Reply)
Discussion started by: Krishanu Saha
1 Replies

3. Shell Programming and Scripting

Help comparing 2 files and sending differences

I have 2 files that need to be compared. Email the differences if something is different and don't email if nothing is different. One or both of the files could be empty. One or both could have data in them. example files backup.doc.$(date +%y%m%d) file size is 0 backup.doc.$(TZ=CST+24... (4 Replies)
Discussion started by: jabbott3
4 Replies

4. Shell Programming and Scripting

Comparing two files and list the differences

Hi * I have two text files which has the file size, timestamp and the file name. I need to compare these two files and get the differences in the output format. Can anyone help me out with this. * cat file1.txt *474742 Apr 18* 2010 sample.log *135098 Apr 18* 2010 Testfile 134282 Apr 18* 2010... (7 Replies)
Discussion started by: Sendhil.Kumaran
7 Replies

5. Shell Programming and Scripting

Perl: Comparing to two files and displaying the differences

Hi, I'm new to perl and i have to write a perl script that will compare to log/txt files and display the differences. Unfortunately I'm not allowed to use any complied binaries or applications like diff or comm. So far i've across a code like this: use strict; use warnings; my $list1;... (2 Replies)
Discussion started by: dont_be_hasty
2 Replies

6. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

7. Shell Programming and Scripting

How to detect difference between files and print the differences

Dear all, I have some problem here. I want to detect the differences between two text files and count the number of differences between them. Such as file1.txt i have : I am new in perl. and file2.txt i have : I ma enw in pearl. So it should return me 3 as the number of differences. So... (2 Replies)
Discussion started by: branred
2 Replies

8. Shell Programming and Scripting

using perl to print columnwise

Suppose we have two files one file FAAA_HUMAN.input1 2 5 7 11 and another file FAAA_HUMAN.output M*0.0540*0.0039*0.2212*0.0082*0.0020*0.0137*0.0028*0.0029*0.2198*0.0104*0.0889*0.0282*0.0049*0.0804*0.1743*0.0215*0.0531*0.0071*0.0007*0.0021*0.7270*2.5000*... (4 Replies)
Discussion started by: cdfd123
4 Replies

9. Shell Programming and Scripting

Trying to print data row/columnwise

PRDCNT=12 FILS= f1=1 f2=2 f3=3 f4=4 f5=5 CNT=0 CNT=`expr $PRDCNT - 5` MNT=6 VR=1 while do f$VR="$f$VR,$MNT" echo "$f$VR" MNT=`expr $MNT + 1` CNT=`expr $CNT - 1` VR=`expr $VR + 1` if then (8 Replies)
Discussion started by: swaminathanks
8 Replies

10. Shell Programming and Scripting

comparing file content differences

I need to write a script to find out if there are any .c files created/removed from the last time i monitored the files available. i first created a file to contain all the .c files available on the system. (ls *.c > file1) I created another file using the same command. I used the comm file1... (4 Replies)
Discussion started by: RianTan
4 Replies
Login or Register to Ask a Question