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.

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
share|improve this question
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 16 hours ago

5 Answers 5

up vote 17 down vote accepted

Using paste:

paste -d \\n file2 file1
share|improve this answer
    
yes... it's working... thanks a ton –  Nainita 18 hours ago

Another awk solution:

awk '{print; getline < "file1"; print}' file2
share|improve this answer

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
share|improve this answer
    
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 18 hours ago
    
@Nainita That's what you're showing in your example output. –  kos 18 hours ago
    
@Nainita Anyway to switch the order of the output you can just switch file1 and file2 in the command. –  kos 17 hours ago
    
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 17 hours ago
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 15 hours ago

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
share|improve this answer

man cat to inform

cat file1 > file2
share|improve this answer
    
I don't see how this provides the OP's desired output. –  roaima 12 hours ago
    
It will not provide the desired output.... it means it will open the file1 using cat and save to to file2... But that was not the requirement –  Nainita 8 hours ago

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.