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
source to share
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
source to share