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
don_crissti
  • 79,330
  • 30
  • 216
  • 245
Nainita
  • 2,712
  • 12
  • 33
  • 50
  • 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 Jun 17 '15 at 10:32

5 Answers5

28

Using paste:

paste -d \\n file2 file1
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
5

Another awk solution:

awk '{print; getline < "file1"; print}' file2
glenn jackman
  • 84,176
  • 15
  • 116
  • 168
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
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
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
kos
  • 2,827
  • 1
  • 11
  • 19
  • 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 Jun 17 '15 at 08:42
  • @Nainita That's what you're showing in your example output. – kos Jun 17 '15 at 08:46
  • @Nainita Anyway to switch the order of the output you can just switch `file1` and `file2` in the command. – kos Jun 17 '15 at 08: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 Jun 17 '15 at 08:53
  • @Nainita No, it doesn't. Try again: `awk 'NR==FNR {x[FNR]=$0;next} {print x[FNR]"\n"$0}' file1 file2`. – kos Jun 17 '15 at 08:56
  • @kos - if it matters, most of Nainita's other questions have been targeted at a Solaris system. Is it possible your script uses some non-standard syntax which might result as he/she indicates? – mikeserv Jun 17 '15 at 10:04
  • @mikeserv Not that it seems. `FNR` is a `gawk` / `nawk` feature, so if it works it means he/she's not using the original `awk` which lacks it, and if it works one way it should work the other way around also. Unless he/she's using something other than the original `awk`, `gawk` or `nawk` which might lack something else. – kos Jun 17 '15 at 10:24
  • @Nainita Can you provide the version of `awk` you're using? – kos Jun 17 '15 at 10:24
  • @kos - it's probably `mawk` - it's Solaris. – mikeserv Jun 17 '15 at 10:29
  • @mikeserv it is the OP's responsibility to mention the OS. If they don't, it's not up to us to go search the OP's history and guess it. We don't know how many machines they have or if they all use Solaris or what. All we can do is provide answers based on the information we have. If that's not good enough for the OP, it will be for the next person who stumbles upon the answer. – terdon Jun 17 '15 at 10:31
  • @terdon - I guess, but isn't this site all about the OS? – mikeserv Jun 17 '15 at 10:33
  • @mikeserv which is why I left a comment _to the OP_ asking them to clarify. My point is that if the OP can't be bothered to tell us what OS they're using, we can't be expected to guess it from their history. Nor should we, there's no reason to assume they only use one. – terdon Jun 17 '15 at 10:40
  • @terdon - they posted on *Unix and Linux* - it's one or the other - how specific should you have to be? And I didn't make that assumption, eiither. I was just letting kos know, I couldn't upvote the answer again, but I saw the comments here and so I let kos know what I did. What's wrong with that? As to guessing - is it not a guess already if your answer is prohibitive in some way, or limited by OS? – mikeserv Jun 17 '15 at 10:45
  • 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 Jun 17 '15 at 10:55
  • @kos - sweet. That rules that out. Probably was an input error. I still can't upvote you again, though. Sorry. – mikeserv Jun 17 '15 at 10:55
  • @mikeserv Thanks for the first one. I'll settle for it – kos Jun 17 '15 at 11:06
  • @mikeserv I'm not saying you did anything wrong, I'm annoyed at the OP, not you. As you well know, there are important differences both between Linux and Unix and between the various Unices. That's why it's important the OP tell us which they are using. – terdon Jun 17 '15 at 11:17
-2

The easiest solution is given below.

cat file1 >> file2

or

cat file2 >> file1
techraf
  • 5,831
  • 10
  • 33
  • 51
sachin
  • 131
  • 7
  • 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...) – don_crissti Jul 11 '16 at 11:32