Reputation: 2452
If a have a flat file database in which fields are separated by space like this :
Name Salary_cost function
Luc 50000 Engineer in mechanics
Gerard 35000 Bad in all, good at nothing
Martijn 150000 Director
Robert 45000 Java Specialist
(...)
I would like to order this stuff by Salary_cost. I can order this using this kind of stuff
cat file.txt | sed-e 's/ \+/\t/g' | sort -k 2
But this is no good, because
I have thought of something like Recutils. But I cannot grasp how to use it for this purpose.
I can I sort this file by "Salary_cost" fields, considering other lines as records and the first as data header, using a command line interface (bash,sh, ksh,...)?
There is a lot of interfaces which produce such output, for example: df, transmission-remote, ps, ... Even coma separated files are close to this structure.
Upvotes: 2
Views: 200
Reputation: 785691
You can use head, tail combination piped with sort:
fld="Salary_cost"
n=$(awk -v q="$fld" 'NR==1{for (i=1; i<=NF; i++) if ($i==q) {print i; exit}}' file)
head -1 file && tail -n +2 file | sort -nk$n
Name Salary_cost function
Gerard 35000 Bad in all, good at nothing
Robert 45000 Java Specialist
Luc 50000 Engineer in mechanics
Martijn 150000 Director
Upvotes: 1