LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-03-2012, 04:08 PM   #1
NsearchOf
LQ Newbie
 
Registered: May 2009
Posts: 8

Rep: Reputation: 0
Using Linux command diff


Is there a way to merge two files in Linux without losing content in either file? For example
File 1 has:
Apple
Grape
Strawberry
Watermelon

File 2 has:
Green Beans
Corn
Apple
Grape

I need to merge the two so File 1 will have:
Apple
Grape
Strawberry
Watermelon
Green Beans
Corn

I have tried several examples from other posts but I can not seem to get the two files merged. One over writes the other.
My last attempt was using diff and patch

diff file1 file2 <difference
patch -p0 <difference
choose file1 to patch
..the results were it removed Strawberry and Watermelon.

Any help would be greatly appreciated!
 
Old 08-03-2012, 04:19 PM   #2
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
Simplest way is:
Code:
cat file1 >> file2
This will append the contents of file1 to file2. A single > would replace the contents of file1 with those of file2.
 
Old 08-03-2012, 04:55 PM   #3
kaiserkarl13
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora
Posts: 23

Rep: Reputation: 4
The solution using cat will duplicate the "Apple" and "Grape" entries, like so:
Apple
Grape
Strawberry
Watermelon
Green Beans
Corn
Apple
Grape

I assume that is not what the OP wanted. You can sort the list, then remove duplicates, like this:
Code:
sort -u file1 file2
which returns:
Apple
Corn
Grape
Green Beans
Strawberry
Watermelon

Not quite what you stated you wanted, but probably as good as you're going to do without writing a more complicated program. Which can be done; if you still want that, I'd look into using awk.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] need some help on using diff command kingston Linux - Newbie 5 01-13-2011 05:37 AM
Diff Command is possible for me? admod Linux - Virtualization and Cloud 1 11-21-2010 10:42 AM
How to use 'diff' command ursusman Linux - Newbie 1 03-08-2007 05:29 AM
help on diff command anandss2004 Programming 2 11-01-2005 01:58 PM


All times are GMT -5. The time now is 06:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration