Sort question.

Hi Guys,
I have a file to be sorted and uniq with the following format.

S00001002|01|20021231
S00001002|01|20011031
S00001006|01|20120430
S00001006|01|20111231
S00001006|01|20111031
S00001006|01|20110831
S00001006|01|20110731
S00001006|01|20101231
S00001006|01|20091231
S00001006|01|20081231
S00001006|01|20071231
S00001006|01|20071130
S00001006|01|20061231
S00001006|01|20061031
S00001006|01|20051231
S00001006|01|20011130
S00001006|01|20010731
S00001009|01|20010430
S00001023|01|20090430

The desired output is

S00001002|01|20021231
S00001006|01|20120430
S00001009|01|20010430
S00001023|01|20090430

I am trying to use the sort and uniq command for the first 13 characters.

sort -k 1,13 file1 | uniq  > file2

Looks like I am missing something in the sort command here.Can you please help.

Try:

sort file1 | uniq -w13 > file2

@bartus11 : uniq -w doesn't work on my system since I am using HP unix.

You need to tell it your field separator. I am assuming a "|" pipe based on your data format. Try the following:

sort -u -t"|" -k1 file1 > file2

Try this then:

sort file | awk 'BEGIN{FS="|"}!a[$1]++'
1 Like

Thanks bartus11!! The awk command did the trick..:smiley:

Appreciate your quick response!