Find the answer to your Linux question:
Results 1 to 7 of 7
Hi Guys, Im trying to create a backup mailserver for some cpanel servers. This involves a file on backup server (/etc/secondarymx) containing the contents of a file (/etc/localdomains) from each ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    2

    I need to merge two files, anyone know how?


    Hi Guys,

    Im trying to create a backup mailserver for some cpanel servers.

    This involves a file on backup server (/etc/secondarymx) containing the contents of a file (/etc/localdomains) from each other server wishing to use it.

    I can copy the "localdomains" file over to the backup server which is fine but how do I merge the file contents to the "secondarymx" file but not duplicate data.

    The files are just domain lists with line breaks i.e.
    domain1
    domain2
    domain3...

    Thanks,

    Paul

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,879
    A bit of shell script using grep should do it, the general process is this:

    assume your 2 files are file1 and file2

    Copy file1 to outfile

    for each line file2

    if grep for line in outfile produces no results

    append the line to the end of outfile

    endif

    next line in file2
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Just Joined!
    Join Date
    Nov 2005
    Posts
    2
    Quote Originally Posted by Roxoff
    A bit of shell script using grep should do it, the general process is this:

    assume your 2 files are file1 and file2

    Copy file1 to outfile

    for each line file2

    if grep for line in outfile produces no results

    append the line to the end of outfile

    endif

    next line in file2
    problem is i cant code

  4. #4
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Romania
    Posts
    186
    Code:
    cat file1 file2 .. fileN  > mergedfile
    Note the '>' in the command: it points that the target file follows, don't forget to put it there.
    You can only be young once. But you can always be immature.

  5. #5
    Just Joined!
    Join Date
    Sep 2005
    Posts
    5
    If you accept that entries in the resulting file are sorted, you can use uniq to remove duplicates.
    Code:
    cat file1 file2 | sort | uniq > mergedfile

  6. #6
    Linux Newbie user-f11's Avatar
    Join Date
    Feb 2011
    Location
    Sofia, BG
    Posts
    185
    Quote Originally Posted by sharkyro View Post
    Code:
    cat file1 file2 .. fileN  > mergedfile
    Note the '>' in the command: it points that the target file follows, don't forget to put it there.
    Hi,
    I tried this command with 4 .PDF files, but only the last one remains in the target - pls. advise.

  7. #7
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,733
    Welcome to the forums, user-f11!

    This thread is working its way to being 6 years old. Please feel free to start a new thread of your own with all the details if you are having any problems with Linux.

    Thank you.

    Locking this thread...
    oz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •