I need to sort horizontally, not vertically, and sort
seems to be designed for vertical operation. q E.g., I have three rows as follows:
banana/orange/apple/mango
potato/tomato/onion/garlic
chair/table/carpet/window
All rows have the same number of columns, separated by a /
. I want the columns to be re-arranged according to alphabetical order in row 1. So it will become:
apple/banana/mango/orange
onion/potato/garlic/tomato
carpet/chair/window/table
This might be better visualized as shown below:
i.e., like sort-by-column in a spreadsheet.
Best Answer
With GNU
awk
, you may dictate the order in which arrays are traversed by settingsorted_in
withinPROCINFO
. In this instance, set it to@val_str_asc
to force arrays to be traversed in ascending order of values.Next split the first line into an array
a
Finally, for each line, traverse the array and print fields corresponding to the keys as they are retrieved.Or, with
python