CaseWorkerName is column 11. The
sort utility may be told what column to sort by, and what delimiter to use when specifying columns:
$ sort -t ',' -k11,11 data.in
This will tell
sort to use commas as delimiters and sort in ascending lexicographical order based column 11 to 11 (i.e. only column 11).
The output is written to the console. If you want to store the output in another file, use
$ sort -t ',' -k11,11 -o data.out data.in
It is ok to specify
data.in as output file if you use the
-o flag to do it.
If you wish to avoid sorting the first line (it may contain the column headers), then we must first separate the header from the data:
$ head -n 1 data.in >data.header
$ sed '1d' data.in >data.unsorted
Then sort and reunite the sorted data with the header:
$ sort -t ',' -k1,11 -o data.sorted data.unsorted
$ cat data.header data.sorted >data.out
$ rm data.sorted data.header data.unsorted
$ sed '1d' data.in | sort -t ',' -k11,11 -o data.sorted
$ head -n 1 data.in | cat - data.sorted >data.out
$ rm data.sorted
If you're on a Linux system using GNU coreutils, this may be made even shorter,
$ ( head -n 1; sort -t ',' -k11,11 ) <data.in >data.out
With the GNU coreutils implementation of
head, the standard input to the subprocess (
(...)) will be consumed first by
head while any remaining data is given to
sort. The output from the subprocess will be the output from
head followed by the output from
On other systems,
head might consume more than expected from the standard input stream, which will give
sort nothing (or at least not the rest of the file) to work with. This is the case on at least OpenBSD.
The result is in
data.out after each example above.
Note, all of these approaches will fail if the data in any column contains a comma...