Using the sort -k to format a txt file

2710 views bash
6

I am stuck using the sort command to output students.txt into 4 seperate columns, I have tried going the route of sort -k 4n but still does not work. I want to be able to output the students.txt into 4 columns of data under the headings of the first awk statement.

awk 'BEGIN{printf "%-12s %-9s %-12s %-12s\n", "Family Name", "initials", 
"Interviewer Initials", "Interview Time"}'
awk '{printf "%-12s %-9s %-12s %-12s\n", $1,$2,$3,$4}' | students.txt | sort - 
k3.4

answered question

This question needs improvement. You don't give an example of students.txt, nor have you explained what formatting you mean to achieve from sort. Do you want it in some particular order?

Sort, doesn't work well with multiple separator, i.e. several spaces can't be considered a separator.

Welcome to SO, add samples for input and output with code tags in your post too.

1 Answer

6

You need to give sort the separtor. In your case the comma sign. Here is how to sort the 3 column of a comma separated input.

~ $ cat example.txt 
foo,bar,baz
aoo,bug,bug
foo,zzz,boo

$ sort -t, -k 3 example.txt 
foo,bar,baz
foo,zzz,boo
aoo,bug,bug

posted this

Have an answer?

JD

Please login first before posting an answer.