views:

91

answers:

2

What's the easiest/quickest way to interleave the lines of two (or more) text files? Example:

File 1:

line1.1
line1.2
line1.3

File 2:

line2.1
line2.2
line2.3

Interleaved:

line1.1
line2.1
line1.2
line2.2
line1.3
line2.3

Sure it's easy to write a little Perl script that opens them both and does the task. But I was wondering if it's possible to get away with fewer code, maybe a one-liner using Unix tools?

+9  A: 
paste -d '\n' file1 file2
codaddict
+1  A: 
cat file1 file2 |sort -t. -k 2.1

Here its specified that the separater is "." and that we are sorting on the first character of the second field.

Sujoy
I don't understand how this works. For example, there is no colon in the sort input? Can you explain more?
my bad, changed the ':' to '.' its just the separator, if not specified sort uses blank to separate fields. `man sort` for more info.
Sujoy
This answer assumes the input actually takes the literal form described in the question. I think that was supposed to be illustrative. It would be possible to transform each input file that way, but it would be way more passes through the data. codaddict's answer is better.
Novelocrat
@Novelocrat: Yep, this is just another way to do it :)
Sujoy