Comparing two files and appending only missing lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two files and appending only missing lines.
# 1  
Old 04-16-2008
Comparing two files and appending only missing lines.

Hi All,
I am a newbie to Shell scripting. Please help me with the Following problem,

1. I have two files with the same name in different locations in the same machine.
Eg: /root/testfolder/a ---- location 1
/tmp/testfolder/a ----- location 2

2. I want to compare the files in location 1 and location 2.
Eg: diff /root/testfolder/a /tmp/testfolder/a
(Please suggest the best way using diff )

3. I must append only the lines missing in location 1(i.e. /root/testfolder/a) without losing any content of location 1.

Eg:
File in location 1Smilie/root/testfolder/a)

Added line in Location 1
This is a test Script
But i am stuck.Please help
This is Added line to LOCATION 1(/root/testfolder/a)

File in location 2Smilie/tmp/testfolder/a)
Added Line in the Location 2 (/tmp/testfolder/a)
This is a test Script
But i am stuck.Please help
This is added line for testing in LOCATION 2(/tmp/testfolder/a)


Executing diff command:

diff /root/testfolder/a /tmp/testfolder/a

Say output of the diff command looks like this:

1c1
< Added line in Location 1 (/root/testfolder/a)
---
> Added Line in the Location 2 (/tmp/testfolder/a)
4c4
< This is Added line to LOCATION 1(/root/testfolder/a)
---
> This is added line for testing in LOCATION 2(/tmp/testfolder/a)


Here,

I am concerned only to append those lines starting with ">" to the correct lines of /root/testfolder/a

Expected result in /root/testfolder/a

Added Line in the Location 2 (/tmp/testfolder/a)
Added line in Location 1
This is a test Script
This is added line for testing in LOCATION 2(/tmp/testfolder/a)
But i am stuck.Please help


Please help me develop a shell script to solve the above posted problem.
Thanks in advance
Karthick.
# 2  
Old 04-16-2008
Is the order of the lines important? Or can you just add the missing lines to the end of the file? If so, you could simply

Code:
fgrep -vxf file2 file1 >>file2

assuming you have an fgrep which understands the -f option.
# 3  
Old 04-16-2008
yes the order of files is important. i want to compare perl source code files.


Thanks,
Karthick
# 4  
Old 04-16-2008
Have you tried diff3? Do you have access to a common descendant which has none of the changes from either file?

I would suggest diff -u and patch but it's probably much easier if you can work against an earlier version of the file.
# 5  
Old 04-17-2008
I can't really understand patch command... and how it can help me. ....Plz explain me how i can use it?

I also dint understand about "working against earlier version of file"
# 6  
Old 04-17-2008
Let's say we have a file. Call it "version 1". Then you copy it to another place, and make some edits. Call this "version 2a". Then copy it to yet another place, and make some different edits. Call this "version 2b".

Now, the simple way to merge changes from 2a to 2b is to find the difference between 1 and 2a, and apply that to 2b.

Code:
diff -u version1 version2a >diff2a
cd ../path/to/2b
patch -p0 <../back/to/diff2a

The patches produced by diff have absolute line numbers etc so you cannot blindly apply them. patch is able to introduce "fuzz" so it can squeeze a patch into place even if some changes have happened in the file.

If there are conflicts, of course, you still need to resolve them.

A version control system probably helps if you need to do this often.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

2. Shell Programming and Scripting

File comparing and appending based on fields

I want to compare 2 files, locus_file.txt is a very large file and attr.txt is a small file. I want to match the first 2 columns of the first file to the second column of attr.txt and print the attributes together. locus_file.txt:large file LOC_Os02g47020, LOC_Os03g57840,0.88725114... (3 Replies)
Discussion started by: Sanchari
3 Replies

3. Shell Programming and Scripting

Comparing 2 text files & downloading a file if the last lines are different

Hello I'm having a little difficulty in writing a shell script for a few simple tasks. First I have two files "file1.txt" and "file2.txt" and I want to read and compare the last line of each file. The files look like this. File1.txt File2.txt After comparing the two lines I would... (2 Replies)
Discussion started by: RustikGaming
2 Replies

4. Shell Programming and Scripting

Comparing lines of two different files

Hello, Please help me with this problem if you have a solution. I have two files: <file1> : In each line, first word is an Id and then other words that belong to this Id piMN-1 abc pqr xyz py12 niLM y12 FY4 pqs fiRLym F12 kite red <file2> : same as file1, but can have extra lds... (3 Replies)
Discussion started by: mira
3 Replies

5. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

6. Shell Programming and Scripting

Omit Blank Lines while comparing two files.

Hello All, I am writting file comparison Utility and I have encountered such a senario where there are 2 files such as follows- 1#!/usr/local/bin/python 2 import gsd.scripts.admin.control.gsdPageEscalate 3.gsd.scripts.admin.control.gsdPageEscalate.main() 1 #!/usr/local/bin/python... (10 Replies)
Discussion started by: Veenak15
10 Replies

7. Shell Programming and Scripting

Comparing 2 files and return the unique lines in first file

Hi, I have 2 files file1 ******** 01-05-09|java.xls| 02-05-08|c.txt| 08-01-09|perl.txt| 01-01-09|oracle.txt| ******** file2 ******** 01-02-09|windows.xls| 02-05-08|c.txt| 01-05-09|java.xls| 08-02-09|perl.txt| 01-01-09|oracle.txt| ******** (8 Replies)
Discussion started by: shekhar_v4
8 Replies

8. Shell Programming and Scripting

comparing lines from 2 files

Hi Friends, I have 2 files A and B . I want to compare the 3rd line of file A and B . (I dont want to compare the 2 files, using diff or cmp). I just want to know whether 3rd line of A matches the 3 rd line of B. Can anybody share their knowledge on the same? Thanks , Vijaya (12 Replies)
Discussion started by: vijaya2006
12 Replies

9. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies

10. Shell Programming and Scripting

Appending Consecutive lines

Hi, I have a file containing a single field on every row. What I need is to append one on to the end of another, e.g. The input file looks like this: nnnnn mmmmmm nnnnn mmmmmm I need it to look like this: nnnnn mmmmmm nnnnn mmmmmm Any ideas would be much appreciated,... (8 Replies)
Discussion started by: pondlife
8 Replies
Login or Register to Ask a Question