9

File1:

.tid.setnr := 1123 
.tid.setnr := 3345 
.tid.setnr := 5431
.tid.setnr := 89323

File2:

.tid.info := 12
.tid.info := 3
.tid.info := 44
.tid.info := 60

Output file:

.tid.info := 12
.tid.setnr := 1123
.tid.info := 3
.tid.setnr := 3345
.tid.info := 44
.tid.setnr := 5431
.tid.info := 60
.tid.setnr := 89323
1
  • 2
    Please always mention your operating system. A lot of the standard tools behave differently on the different OSs so we need to know what you're using.
    – terdon
    Commented Jun 17, 2015 at 10:32

5 Answers 5

28

Using paste:

paste -d \\n file2 file1
0
5

Another awk solution:

awk '{print; getline < "file1"; print}' file2
5

The paste solution is the most portable and most efficient. I'm only mentioning this alternative in case you prefer its behaviour in the case where the two files don't have the same number of lines:

With GNU sed:

sed Rfile1 file2

If file1 has fewer lines than file2, then when file1 is exhausted, sed will not output anything for it (as opposed to empty lines for paste).

If file1 has more lines than file2, then those extra lines will be discarded (as opposed to printing empty lines for file2 with paste).

$ paste a b
1       a
2       b
3
4
$ paste -d \\n a b
1
a
2
b
3

4

$ sed Rb a
1
a
2
b
3
4
$ sed Ra b
a
1
b
2
4

Using awk (gawk, nawk, mawk):

awk 'NR==FNR {x[FNR]=$0;next} {print x[FNR]"\n"$0}' file2 file1 > outputfile
  • NR==FNR {x[FNR]=$0;next}: NR==FNR is matched only if the current record number is equal to the current file record number (hence it's matched only while processing the first file): stores the currently processed record into the array x at an index equal to the current file record number and skips the current record
  • {print x[FNR]"\n"$0}: prints the content of the array x at an index equal to the current file record number followed by a newline and by the content of the current record
~/tmp$ cat file1
.tid.setnr := 1123
.tid.setnr := 3345
.tid.setnr := 5431
.tid.setnr := 89323
~/tmp$ cat file2
.tid.info := 12
.tid.info := 3
.tid.info := 44
.tid.info := 60
~/tmp$ awk 'NR==FNR {x[FNR]=$0;next} {print x[FNR]"\n"$0}' file2 file1
.tid.info := 12
.tid.setnr := 1123
.tid.info := 3
.tid.setnr := 3345
.tid.info := 44
.tid.setnr := 5431
.tid.info := 60
.tid.setnr := 89323
17
  • It's giving the output but not exactly the same what I wanted. tid.info lines are coming after tid.setnr lines in my output file.
    – Nainita
    Commented Jun 17, 2015 at 8:42
  • @Nainita That's what you're showing in your example output.
    – kos
    Commented Jun 17, 2015 at 8:46
  • @Nainita Anyway to switch the order of the output you can just switch file1 and file2 in the command.
    – kos
    Commented Jun 17, 2015 at 8:48
  • Yes...I have done the same but it was printing the exactly as before . after printing tid.setnr then it was priting tid.info.
    – Nainita
    Commented Jun 17, 2015 at 8:53
  • 1
    @mikeserv However since I was at it I tried mawk also, and it runs on it as well. Anyway being reasonable I can't see why it shouldn't work just the other way around (i.e. just by switching files). It's not that awk cares about the input, lines are lines. If something wasn't supported by his version it would have just broke the first time. Way more easily, simply OP did a mistake switching the input files in the arguments.
    – kos
    Commented Jun 17, 2015 at 10:55
-2

The easiest solution is given below.

cat file1 >> file2

or

cat file2 >> file1
1
  • 1
    sachin, read the question again; this appends the content of one file to the content of another file. It does not merge the files alternating the lines (so one line from file1 then one line from file2 and so on...) Commented Jul 11, 2016 at 11:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .