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 tried to get the 2 files output in a single file. I have tried below command:

cat file1 file2

but here file2 data is appending to file1.

What I need is file1 1st line followed by file2 first line, and file1 second line followed by file2 second line etc.

Can anybody tell me how to do this in a simple way?

share|improve this question

2 Answers 2

There's an app for that!

$ cat file1
file1 line1
file1 line2

$ cat file2
file2 line1
file2 line2

Now, if you pass these files as arguments to paste:

$ paste -d' ' file1 file2
file1 line1 file2 line1
file1 line2 file2 line2

If by "file1 1st line followed by file2 first line, and file1 second line followed by file2 second line etc.", you mean that you want a new line between each line pasted, simply adjust the -d (delimiter) option.

$ paste -d'\n' file1 file2
file1 line1
file2 line1
file1 line2
file2 line2
share|improve this answer
    
+1 for "There's an app for that!" –  Celada Jul 3 at 19:42
paste -d '\n' file1 file2 > file3

where file1 and file2 are the input files and file3 will be the created output.

share|improve this answer
    
Welcome to the U&L Stack Exchange! This answer is almost identical to terdon's answer. In cases where you think you can improve on an answer, but don't provide substantially new information, please considering proposing an edit to an existing answer. Answers are also reordered based on voting, so each answer should stand on its own or refer to other answer. That's why shell oneliners should be accompanied by a description that explain their operation. –  Thomas Nyman Sep 25 at 11:26

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.