Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split up a line and rearrange its elements?

I have some data on a single line like below

abc edf xyz rfg yeg udh

I want to present the data as below

abc
xyz
yeg


edf
rfg
udh

so that alternate fields are printed with newline separated. Are there any one liners for this?

like image 503
Vijay Avatar asked Dec 18 '25 02:12

Vijay


1 Answers

The following awk script can do it:

> echo 'abc edf xyz rfg yeg udh' | awk '{
    for (i = 1;i<=NF;i+=2){print $i}
    print "";
    for (i = 2;i<=NF;i+=2){print $i}
    }'
abc
xyz
yeg

edf
rfg
udh
like image 199
paxdiablo Avatar answered Dec 20 '25 17:12

paxdiablo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!