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
# 1  
Old 11-08-2011
Compare and merging the differences in text file

Hi

i have gone through some sdiff command it shows the differences side by side and its really awesome

Code:
file 1:
this tool is for 
checking the differ
merging with flower pots documentation

Code:
file 2:
this t   ool is for 
checking the differ
mergin    g with flower pots documentation

we can clearly say that there is difference in line 1 and line 3 of file 2

Code:
diff -e file2 file1 > output

$ diff -e file2 file1 > output
$ (cat output && echo w) | ed - file2

file1 and file2 are exactly same

1 ) i want to include a flower pot documentation like below example


Code:
line 1,3 of file 2 is changed 

file2 should look like this:


************** my change  start point *********************
this tool is for
************** my change  end point *********************
checking the differ
************** my change  start point *********************
merging with flower pots documentation
************** my change  end point *********************

start and end tags are fixed strings , we can assign them to a STRING1 and STRING2 but how can we print while merging

any help is much appreciated , thanks

Last edited by rakeshkumar; 11-08-2011 at 04:14 PM..
# 2  
Old 11-08-2011
Code:
nawk 'FNR==NR{f2[FNR]=$0;next} {if ( $0 == f2[FNR]) print; else printf("%s\n%s\n%s\n", s,$0,e)}' s='** my change start point **' e='** my change end point **' file2.txt file1.txt

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-08-2011
Hi Vgersh99

i could not understand your code but its working perfectly on terminal , really great coding

can you please explain below code and how does it taking pointers to two file same time

Code:
nawk 'FNR==NR{f2[FNR]=$0;next}[


Last edited by rakeshkumar; 11-08-2011 at 05:49 PM.. Reason: rework
# 4  
Old 11-08-2011
FNR==NR{f2[FNR]=$0;next}
This piece of code gets executed when processing the first file specified on a command line - in your case it's file2.txt
It populates an array f2 index my current record/line number (which is stored in the variable FNR) with the entire current record/line (store in $0).

The rest of the script simply compares the corresponding lines from file1.txt with previously array stored lines from file2.txt.

Last edited by vgersh99; 11-08-2011 at 06:32 PM..
# 5  
Old 11-09-2011
thanks vgersh

i want to make the output like below
Code:
************** my change  start point ********************* 
*this t   ool is for   (Note: this is old code,* at beginning indicates old code is commented ) 
this tool is for
************** my change  end point ********************* 
checking the differ 
************** my change  start point ********************* 
*mergin    g with flower pots documentation
merging with flower pots documentation 
************** my change  end point *********************

Code:
nawk 'FNR==NR{f2[FNR]=$0;next} {if ( $0 == f2[FNR]) 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

i tried with below code but it prints the same line twice where as i want the line1,3 from file2 needs to be printed but it prints line1,3 from file1
Code:
************** my change  start point ********************* 
this tool is for 
this tool is for 
************** my change  end point ********************* 
checking the differ 
************** my change  start point ********************* 
merging with flower pots documentation 
merging with flower pots documentation 
************** my change  end point *******************


Last edited by rakeshkumar; 11-09-2011 at 02:54 AM.. Reason: incorrect format
# 6  
Old 11-09-2011
Code:
nawk 'FNR==NR{f2[FNR]=$0;next} {if ( $0 == f2[FNR]) print; else printf("%s\n*%s\n%s\n%s\n", s,f2[FNR],$0,e)}' s='** my change start point **' e='** my changxe end point **' file2.txt file1.txt

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 11-09-2011
thanks lot vgersh ,

why $f2[FNR] not working but $0 works ?

one more question using sdiff with my prog1 and prog2 shows lots of differences which also considering the space , i went through the man pages of sdiff and found -W flag
Code:
sdiff -W prog1 prog2 shows only differences which i want ignoring the white spaces


how can we implement the same flag in Awk , i went through manual pages of awk but it shows alot of things with -W

Last edited by rakeshkumar; 11-09-2011 at 06:52 AM..
 
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