merging two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging two files
# 8  
Old 06-29-2004
Code:
firstfile >> secondfile

That's going to do naff all unless firstfile is an executable, in which case the output (stdout) from firstfile will be appended to secondfile.

You'll need
Code:
cat firstfile >> secondfile

Cheers
ZB
# 9  
Old 06-29-2004
Thanks guys for all of your help.

what PaulC is exactly what i want.... I am giving an example data set here ... Ygor ur comm seems to point me in right direction but its not what i want..


At first both files contain

this is mango
this is apple
this is orange

After sometime first file will have inserts anywhere in the file (middle, last, etc..) and second file will have inserts only at the end of the file. so after sometime files might look like

file1

this is mango
this is banana
this is apple
this is grape
this is pineapple
this is orange
this is watermelon

file2

this is mango
this is apple
this is orange
this is cherry
this is lemon

OUTPU should be

this is mango
this is banana
this is apple
this is grape
this is pineapple
this is orange
this is watermelon
this is cherry
this is lemon


comm command seems to work differntly...more over it needs data to be sorted which i dont want to do.


So if any of you has any idea please pass it to me...thanks guys once again.
# 10  
Old 06-29-2004
I think comm is what you want.

If you do the following

comm -12 firstfile secondfile >> thirdfile
comm -23 firstfile secondfile >> thirdfile
comm -13 firstfile secondfile >> thirdfile

That will give you lines common in both files, followed by lines
appearing only in the first file, followed by lines appearing only
in the second file.

Cheers
ZB
# 11  
Old 06-29-2004
Perhaps use diff, but reformat the output...

diff file1 file2 | awk '/^[<>]/{print subst($0,3)}'
# 12  
Old 06-29-2004
if you do not mind outfile being in sorted order you can try :
Code:
cat file? | sort | uniq > outfile

peace...
# 13  
Old 06-29-2004
making things too hard

hello,

there are many ways to skin a cat. All good suggestions, however, the file cannot be sorted. One of the files will only have appended data , meaning the data is attached to the end of the file only. The other file will have data inserts that could appear all over the file. From the example given:

file1

this is mango
this is banana
this is apple
this is grape
this is pineapple
this is orange
this is watermelon

file2

this is mango
this is apple
this is orange
this is cherry
this is lemon

OUTPUT should be

this is mango
this is banana
this is apple
this is grape
this is pineapple
this is orange
this is watermelon
this is cherry
this is lemon

----------------------------------

the one solution given by appending the second file appears to be what you want. That is if this example holds true. The implementation needs a little going over.

If only one file, file1 will have modified data inserts all over its contents. This can be your primary file.
The second file does not really need to start off with any content since the only data modifications to be performed are to be done on the EOF. New data appended to back of file.
If you want to keep a copy of the original file then simply have three (3) files instead of two (2)

When the event happens that you merge these files say a specific time sceduled in cron or by size or what ever it is you simply take the 3rd files contents , which are the records originally appended to the back of the secondary file. ( which now is just a copy of the original file ) a 3rd file has the new records.
then simply append file2 to file1.


--------------------
original data
----
this is mango
this is apple
this is orange

--------------------
file 1 & file 2 look like

this is mango
this is apple
this is orange
-------------------------
file 3
null
-------------------------
file1 after inserts

this is mango
this is banana
this is apple
this is grape
this is pineapple
this is orange
this is watermelon

file2-----------------
simply a copy or dummy file showing appends
- don't really need except for archive purposes

this is mango
this is apple
this is orange
[this is cherry]
[this is lemon]

-------------------------------------
3rd / ( or 2nd ) file
this is cherry
this is lemon

OUTPUT file ( cat file2 >> file1 )

this is mango
this is banana
this is apple
this is grape
this is pineapple
this is orange
this is watermelon
this is cherry
this is lemon


Smilie
# 14  
Old 06-29-2004
Thanks a lot guys ... for so many replies.


Gary solutiioon is a good one. I think for now it works....

But i have a question what if we have been already given files in that format meaning we dont have control over the files from start...so then how anybody is gonna solve....

zazzybob solution also should work hypothetically but for some reason comm command is behavnig strangly... Its not giving what it should...

Once again thanks for all suggestions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two files

Hi All , I have a scenario where we need to combine two files . Below are the sample files and expected output , File 1: 1|ab 1|ac 1|ae 2|ad 2|ac File 2: 1|xy 1|fc 2|gh 2|ku Output file : 1|ab|xy (3 Replies)
Discussion started by: saj
3 Replies

2. Shell Programming and Scripting

Merging two files

Guys, I am having little problem with getting a daily report! The daily process that I do is as follows 1. Unload Header for the report from the systables to one unl file, say Header.unl 2. Unload the data from the required table/tables to another unl file, say Data.unl 3. Send a... (2 Replies)
Discussion started by: PikK45
2 Replies

3. Shell Programming and Scripting

Help with merging 2 files into 1

::::::::: ::FileA:: ::::::::: A1-------A2--------A3---A4---A5-- ================================= AC5VXVLT-XX---------------------- B57E434--XXXX1-----MMMM-ZZZ--111- C325G20--XXXXX3----CCCC------3332 DC35S51--XXXXY1----DDDD------44X- DC35S52--XXXXY2----DDDD------44Y-... (5 Replies)
Discussion started by: lordsmiter
5 Replies

4. Shell Programming and Scripting

Merging two files with same name

Hello all, I have limited experience in shell scripting. Here goes my question: I have two directories that have same number of files with same file names i.e. consider 2 directories A and B. Both directories have files 1.txt, 2.txt...... I need to merge the file 1.txt of A with file 1.txt... (5 Replies)
Discussion started by: jaysean
5 Replies

5. Shell Programming and Scripting

Merging files

I have two files file 1 containing x rows and 1 column file 2 containing x rows and 1 column I want to merge both the files and add a comma between the two eg plz guide (1 Reply)
Discussion started by: test_user
1 Replies

6. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

7. Shell Programming and Scripting

merging of files.

Hi, I want to merge the two files on the basis of columns like... file 1 Data Key A 12 B 13 file2 Data Value A A1 A A2 B B1 B B2 (5 Replies)
Discussion started by: clx
5 Replies

8. Shell Programming and Scripting

Help with merging files

i would like to merge two files that have the same format but have different data. i would like to create one output file that contains information from both the original files.:rolleyes: (2 Replies)
Discussion started by: joe black
2 Replies

9. Shell Programming and Scripting

Merging 2 files

Hi, I have got two files 1.txt 1111|apple| 2222|orange| 2.txt 1111|1234|000000000004356| 1111|1234|000000001111| 1111|1234|002000011112| 2222|5678|000000002222| 2222|9102|000000002222| I need to merge these two so that my out put looks like below: Search code being used should be... (4 Replies)
Discussion started by: jisha
4 Replies

10. Shell Programming and Scripting

merging files

Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ... (6 Replies)
Discussion started by: vakharia Mahesh
6 Replies
Login or Register to Ask a Question