Terry
Terry

Reputation: 750

how to sort a file with multi-character delimiter using linux shell sort?

I have a file, its delimiter is "|||".

abc|||123|||999|||5|||Just for you | Jim|||20
cef|||7|||210|||6|||Go away | R&B|||30
mmm|||89|||320|||16|||Traveling Light|George Winston|||21

The delimiter "|||" can't be replace with "|" or "||", because data itself may contain "|" or "||".

Could someone tell me how to sort column 2 with delimiter "|||" ?

The following method fails:

sort -t$'|||' -nrk2 a.txt > b.txt
sort: multi-character tab `|||'

Thank you!

Upvotes: 1

Views: 2474

Answers (1)

m13r
m13r

Reputation: 2731

You could change the delimiter to | then sort it with sort and then change everything back:

# change | to __BAR__ writing the result to b.txt
sed 's@\([^|]\)|\([^|]\)@\1__BAR__\2@g' a.txt > b.txt
# change ||| to | in b.txt
sed -i 's@|||@|@g' b.txt

# do sorting with | delimiter writing the result to c.txt
sort -t$'|' -nrk2 -k3,rn -k4,rn b.txt > c.txt 

# change everything back in c.txt:
# | to |||
sed -i 's@|@|||@g' c.txt
# __BAR__ to |
sed -i 's@__BAR__@|@g' c.txt

Upvotes: 1

Related Questions