1

I want to sort following strings by the number and remove duplication in a file

cat311
celine434
celine434
celine5
jimmy12
john44
john41

to be

celine5
jimmy12
john41
john44
cat311
celine434

1 Answer 1

1

You could insert a , (or any character which otherwise doesn't occur in the input) before the first digit, and then sort on the part after that ,:

$ <file sed 's/[0123456789]/,&/' | sort -ut, -k2n -k1,1 | tr -d ,
celine5
jimmy12
john41
john44
cat311
celine434
3
  • This is awesome! I need to learn more about sed! Thank you! Commented Sep 23, 2022 at 19:32
  • What does "-k1,1" do here? I find following also give the same output. <file sed 's/[0123456789]/,&/' | sort -ut, -k2n | tr -d , Commented Dec 19, 2022 at 7:54
  • @user8090410, it adds the first field (the username) as the secondary sorting key. Try on the output of printf '%s\n' john1 joe1 for instance to see the difference. Commented Dec 19, 2022 at 8:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .