Compare and merging the differences in text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare and merging the differences in text file
# 8  
Old 11-09-2011
a brute-force approach:
Code:
nawk '
  BEGIN {
    tab=sprintf("\t")
    space="([ ]*|" tab "*)"
   }
   FNR==NR{f2[FNR]=$0;next}
   {
    s1=$0; s2=f2[FNR]
    gsub(space, "",s1);gsub(space, "",s2)
    if ( s1 == s2) print
        else printf("%s\n*%s\n%s\n%s\n", s,f2[FNR],$0,e)
   }' s='** my change start point **' e='** my change end point **' file2.txt file1.txt

# 9  
Old 11-09-2011
PHP

thanks vgersh

even this approach is also taking the changes of blanks lines and spaces , please see the output ( actual file where am running it)

Code:
Note : the below is the only correct change i needed there is difference is 132 in file2 and 131 in file1 which is reflected
      ******** 90339 ******* Z001590 ******* 07/2011 ***** START ***
      *005400     05  OUTPUT-LINE             PIC X(132).
       005400     05  OUTPUT-LINE             PIC X(131).
      ******** 90339 ******* Z001590 ******* 07/2011 *****  END *****
Note : line is replaced even there is no difference 
      ******** 90339 ******* Z001590 ******* 07/2011 ***** START ****
      *007100         10  TO-MM-NUM             REDEFINES TO-MM PIC 99.
       007100         10  TO-MM-NUM             REDEFINES TO-MM PIC 99.
      ******** 90339 ******* Z001590 ******* 07/2011 *****  END ******
      ******** 90339 ******* Z001590 ******* 07/2011 ***** START ****
      *007200         10  TO-DD                 PIC XX.
       007200         10  TO-DD                 PIC XX.
      ******** 90339 ******* Z001590 ******* 07/2011 *****  END ******
      ******** 90339 ******* Z001590 ******* 07/2011 ***** START*****
      *007300         10  TO-YYYY               PIC XXXX.
       007300         10  TO-YYYY               PIC XXXX.
      ******** 90339 ******* Z001590 ******* 07/2011 *****  END ******

i ran my files with sdiff -W it shows the difference , alternatively i tried grep,sed,perl to eliminate the blanks space but nothing works

Note: without -W flag sdiff also behaves the way the awk script is running now

is there any alternate for sdiff -W in Awk ?

thanks alot for you patience and time
# 10  
Old 11-09-2011
strange, it works for my test cases with multiple spaces and/or tabs.
Please attach both files to this thread - or simply a representative parts of BOTH files which you think fail the testing.
Don't copy/paste - use the 'paper-clip' icon on the top row of the reply pane.
# 11  
Old 11-09-2011
i have attached both the file which am actually testing.

please suggest me any method to deal this case.

Last edited by rakeshkumar; 11-09-2011 at 10:18 AM..
# 12  
Old 11-09-2011
The only change I get is this:
Code:
** my change start point **
*005400     05  OUTPUT-LINE             PIC X(132).
005400     05  OUTPUT-LINE             PIC X(131).
** my change end point **

Are you sure you're using the latest code from post #8?
# 13  
Old 11-09-2011
yes except awk instead nawk am using same code you have provided at post 08 , please find the code attached

Code:
sh script.sh file-name 

Note : Since file-name is same in both the folders

awk --version
GNU Awk 3.1.8

nawk is not available on my system


Last edited by rakeshkumar; 11-09-2011 at 10:33 AM..
# 14  
Old 11-09-2011
Hmmm... this looks right.
Try switching $retro and $main ...
What OS are you on?
If on Solaris, try nawk instead of awk.
If you have 'gawk', try that....

Also make sure neither one of your files have ^M-s at the end of the lines.
If they do: tr -d '\015' < oldFile >newFile

Last edited by vgersh99; 11-09-2011 at 10:47 AM..
This User Gave Thanks to vgersh99 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh / AIX - Differences between lists to a text file

This seems pretty simple, but I cant figure it out. I get stumped on the simple things. I am running two commands 1) take a listing a directory of files, and filter out the doc_name (which is in a series of extracted files), and place it in a file. ls -l | awk '{print $9}' | grep... (5 Replies)
Discussion started by: jeffs42885
5 Replies

2. UNIX for Beginners Questions & Answers

Compare two big files for differences using Linux

Hello everybody Looking for help in comparing two files in Linux(files are big 800MB each). Example:- File1 has below data $ cat file1 5,6,3 2.1.4 1,1,1 8,9,1 File2 has below data $ cat file2 5,6,3 8,9,8 1,2,1 2,1,4 (8 Replies)
Discussion started by: shanul karim
8 Replies

3. Shell Programming and Scripting

Compare two big files for differences using Linux

Hello everybody Looking for help in comparing two files in Linux(files are big 800MB each). Example:- File1 has below data $ cat file1 5,6,3 2.1.4 1,1,1 8,9,1 File2 has below data $ cat file2 5,6,3 8,9,8 1,2,1 2,1,4 (1 Reply)
Discussion started by: shanul karim
1 Replies

4. Shell Programming and Scripting

Need to compare the two files and list out differences between the two

Hi, I need to compare the two files and list out difference between the two. Please assist. Best regards, Vishal (2 Replies)
Discussion started by: Vishal_dba
2 Replies

5. Shell Programming and Scripting

Merging lines in a text file

hi, I have a file as below: Name: some_name Date: some_date Function Name: <some_function_name(jjjjjjjjj, fjddddd, gggg, ggg)> Changes:<Change A more of change A> Name: some_name Date: some_date Function Name: some_function_nameB(jjjjjjjjj, fjddddd, gggg, ggg) Changes:Change B... (15 Replies)
Discussion started by: flamingo_l
15 Replies

6. HP-UX

Compare 2 systems to find any differences

Hi there, I have 2 machines running HP-UX. One off these controllers is able to send mail and the other cannot. I have looked at all the settings that I know and coannot find any differences. Is there a way to audit the 2 machinces by pulling all the settings then compare any differences? ... (2 Replies)
Discussion started by: lodey
2 Replies

7. Shell Programming and Scripting

Compare two text files and Only show the differences

Hi experts, I'mvery new to shell scripting and learning it now currently i am having a problem which may look easy to u :) i have two files File 1: Start :Thu Nov 19 10:33:09 2009 ABCDGFSDJ.txt APDemoNew.ppt APDemoOutline.doc ARDemoNew.ppt ARDemoOutline.doc File 2: Start... (10 Replies)
Discussion started by: CelvinSaran
10 Replies

8. Shell Programming and Scripting

Compare File Differences in different directories

Hello, I am new to scripting and have been trying to compare two different directories, but with all the same file names in each directory for file changes. I have been doing it in baby steps and have been doing pretty good, but I have hit a few snags. Test 1 and Test 2 work great, but my... (4 Replies)
Discussion started by: dmaday
4 Replies

9. UNIX for Dummies Questions & Answers

Compare 2 files for a single column and output differences

Hi, I have a column in 2 different files which i want to compare, and output the results to a different file. The columns are in different positions in those 2 files. File 1 the column is in position 10-15 File 2 the column is in position 15-20 Please advise Thanks (1 Reply)
Discussion started by: samit_9999
1 Replies

10. Shell Programming and Scripting

merging few columns of two text files to a new file

hi i need to select a few columns of two txt files and write it to a new file. there is one common field for both of these files. plz help me in this thanks in advance (4 Replies)
Discussion started by: kolvi
4 Replies
Login or Register to Ask a Question