I need some help understanding how the sort command is working in the following examples:
cat testfile
The input data:
thisfilehasduplicates
thissnowhasfallen
thisduckhasdied
thishallwasfull
berthammwasaclown
fredsimmisprimeminister
fredalbaisabinman
janetyceisscottish
janeouseisenglish
janellyriswelsh
The aim here is to sort and deduplicate by the first four characters. I have read that -k1.1,1.4
will achieve this by sorting by the first four characters of the first field. The comma denotes a range of characters with 1.4
meaning, field 1, character 4.
sort -u -t -k1.1,1.4 testfile
I interpret this version as -t
followed by blank to mean that there is no field separator, however I suppose it could mean field separator is whitespace, however it somehow interferes with the -k
and -u
flags and doesn't give me what I want:
berthammwasaclown
fredalbaisabinman
fredsimmisprimeminister
janellyriswelsh
janeouseisenglish
janetyceisscottish
thisduckhasdied
thisfilehasduplicates
thishallwasfull
thissnowhasfallen
sort -u -k1.1,1.4 testfile
This version, without the -t
flag does give exactly what is required, deduplication on the first four characters, at least I think that's what it is doing
berthammwasaclown
fredalbaisabinman
janellyriswelsh
thisduckhasdied
I've read the man pages on my distribution (SunOS 5.10), but I don't fully understand the sections relating to the -k
and -t
flags, especially when using the dot notation in a key specification.