1
votes

I need to split a single column of data in a large file into two columns as follows:

A
B           B A
C    ---->  D C
D           F E
E           H G
F
G
H

Is there an easy way of doing it with unix shell commands and/or small shell script? awk?

3

3 Answers

3
votes
$ awk 'NR%2{s=$0;next} {print $0,s}' file
B A
D C
F E
H G
3
votes

You can use the following awk script:

awk 'NR % 2 != 0 {cache=$0}; NR % 2 == 0 {print $0 cache}' data.txt

Output:

BA
DC
FE
HG

It caches the value of odd lines and outputs even lines + appends the cache to them.

0
votes

I know this is tagged awk, but I just can't stop myself from posting a sed solution, since the question left it open for "easy way . . . with unix shell commands":

$ sed -n 'h;n;G;s/\n/ /g;p' data.txt
B A
D C
F E
H G