Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have 2 files. I want to merge them.

File1 contents:

abc

File2 contents:

def

Combined File should be:

abc 
def
share|improve this question
    
Its should ne new line for def –  Ocean Mar 19 '13 at 6:36

2 Answers 2

This is a useful1 use of cat:

$ cat File1 File2 > CombinedFile

Footnotes:

1. As opposed to the useless use of cat

share|improve this answer
    
I think he meant to take one line each time from both file, and merge them, not to append one file to another. –  warl0ck Mar 19 '13 at 6:57
1  
"Did you mean: useful" :) –  l0b0 Mar 19 '13 at 16:34
    
@warl0ck Then he should have stated that. –  Bernhard Mar 19 '13 at 18:36

This perl scriptlet does this deed:

#!/usr/bin/perl

open my $f1, "<", $ARGV[0];
open my $f2, "<", $ARGV[1];

do {
    $line1 = <$f1>;
    print $line1 if(defined($line1));
    $line2 = <$f2>;
    print $line2 if(defined($line2));
} while(defined($line1) && defined($line2));
while(<$f1>) {
    print;
}
while(<$f2>) {
    print;
}

Call as e.g. perl splice file1 file2 > spliced. Has no error checking, just a thowaway script.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.