user2893381
user2893381

Reputation: 31

How can I combine lines from two files using sed, awk, or other linux commands

I have two files:

file1.txt

apple

orange

banana

file2.txt

red

white

blue

What I would like to end up with is this:

file3.txt

apple

red

orange

white

banana

blue

Any help would be greatly appreciated!

Upvotes: 3

Views: 3337

Answers (3)

sat
sat

Reputation: 14949

In sed,

sed 'R file2.txt' file1.txt > file3.txt

In Bash,

while IFS= read -r lineA && IFS= read -r lineB <&3 ; do echo "$lineA"; echo "$lineB"; done <file1.txt 3<file2.txt > file3.txt

Upvotes: 4

Imagination
Imagination

Reputation: 606

I am adding an awk solution:

awk '1;getline <"file2"' file1 >newfile

this one-liner works for your example.

Upvotes: 2

devnull
devnull

Reputation: 123458

You can use paste:

paste -d'\n' file1.txt file2.txt > file3.txt

Upvotes: 11

Related Questions