bongboy
bongboy

Reputation: 157

multi character separated sort

How can I sort !! delimited records using sort command?

for File1

1!!2!!3
2!3!!3!!1
3!!2!!2

expected output

2!3!!3!!1
3!!2!!2   
1!!2!!3

then

sort -t \!\! -k 3 file1

RESULT:

sort: multi-character tab ‘!!’

why isn't it working?

Upvotes: 0

Views: 1508

Answers (1)

anubhava
anubhava

Reputation: 785058

Multi-character delimiters are not allowed in sort -t but you can just use:

sort -t '!' -k1 file
1!!b!!c
2!!f!!w
4!!e!!e

EDIT: If ! can be there in data itself you can use this trick:

sed 's/!!/\x06/g' file | sort -t $'\x06' -k1 | sed 's/\x06/!!/g'
1!!b!!c
2!!f!!w
4!!e!!e

EDIT2: For doing this in single command use awk:

awk -F '!!' -v k=1 '{a[$k,$0]=$0} 
   END{asort(a, b, "@ind_num_asc"); for (i in b) print b[i]}' file

Upvotes: 1

Related Questions