Linux - Sort based on two field names
I have several txt files "file_i.txt" that look like this:
id, size, price, colour
1, 2, 10, black
2, 8, 5, blue
...
All files contain these fields, but the order of the fields may be different.
I would like to sort each file based on two functions, first the price and then the size. I know that it is possible to use command sort to sort by two fields like this:
sort -k2,3 -k1,2 file_i.txt
However, since the position of the field is different in every file, is there a way to sort based on the field name?
thank
Basically the command sort
should look like this:
sort -k3n -k2n input
where N
in -kN
- key number, and N
means sorting numeric.
sort
cannot be sorted by field names. You need to get the column number somehow. I use awk
for this:
kprice=$(awk -F', ' -vfield=price 'NR==1{for(i=1;i<=NF;i++){if($i==field){print i;exit}}}' a.txt)
ksize=$(awk -F', ' -vfield=size 'NR==1{for(i=1;i<=NF;i++){if($i==field){print i;exit}}}' a.txt)
sort -k"$kprice"n -k"$ksize"n a.txt
I think you need to write the script yourself, say "mySort" and run like:
./mySort "id" "size"
Pseudocode, assuming the first line of the file contains the column name:
read input list
for each element in list, save the position (parsing first line)
apply sort
you can do some preprocessing:
for each file:
- transpose file
- sort by first column (names)
- return transpose
Then all columns will be ordered the same and you can use the standard sort
approach.
File transpose in bash is already allowed in other questions here: SO:
- An efficient way to transpose a file in Bash
- https://unix.stackexchange.com/questions/79642/transposing-rows-and-columns