Reputation: 392
Input
1st row
2nd row
3rd row
4th row
5th row
6th row
Desired Output
1st row 4th row
2nd row 5th row
3rd row 6th row
I tried column
it doesn't seem to work.
I am trying to do it with out a bash script if possible.
Thanks in advance!
Upvotes: 1
Views: 982
Reputation: 7791
I am trying to do it with out a bash script if possible. Thanks in advance!
The pr
utility can do that without the need for scripting, not saying that scripting is not needed but there exist a tool just for that purpose.
pr -t2 file.txt
To use a space to separate the columns.
pr -t2 -s' ' file.txt
See the man pages for pr
for more info.
Upvotes: 0
Reputation: 12877
With awk:
awk 'BEGIN { cnt=1 } /^$/ { cnt++;next } { if (cnt==1) { map1[cnt1++]=$0 } else { map2[cnt2++]=$0 } } END { for (i in map1) { print map1[i]" "map2[i]}}' file
Explanation:
awk 'BEGIN {
cnt=1 # Initialise a column counter to 1
}
/^$/ {
cnt++; # When there is a blank line, increment the cnt variable
next # Skip to the next line
}
{
if (cnt==1) {
map1[cnt1++]=$0 # If cnt is equal to 1, set an array map1, with an index as an incrementing counter (cnt1) and the value as the actual data on the line ($0)
}
else {
map2[cnt2++]=$0 # If cnt not equal to 1, follow the same logic as before but use array map2 instead
}
}
END {
for (i in map1) {
print map1[i]" "map2[i] # Loop through map1, printing the entries as well as the corresponding index entries for map2
};
}' file
Upvotes: 2