1

I'm trying to sort a text file in the descending order based on the last column. It just doesn't seem to work.

cat 1.txt | sort -r -n -k 4,4

ACHG,89.46,0.08,34200
UUKJL,0.85,-15.00,200
NIMJKY,34.35,0.09,17700
TBBNHW,10.24,0.00,4600
JJkLEYE,73.67,0.48,25400

I've tried removing spaces just in case but, hasn't helped. Also, tried sorting by the other fields just to see but, ahve the same problem.

I just can't work out what is wrong with the command I've issued. Please could I request help with this one?

3
  • 1
    sort -t, -rnk 4,4 1.txt should work Commented May 25, 2020 at 7:45
  • ah damn thing.. just couldn't think of the field separator... Assumed the default is comma. Thanks a million. I would've gone on & on trying different things except for the field separator. Commented May 25, 2020 at 7:46
  • 1
    Post it as the answer & I'll accept it. Commented May 25, 2020 at 7:47

1 Answer 1

3

Your command is almost right but it is missing field separator option -t that should set comma as field separator.

This should work for you:

sort -t, -rnk 4,4 1.txt

ACHG,89.46,0.08,34200
JJkLEYE,73.67,0.48,25400
NIMJKY,34.35,0.09,17700
TBBNHW,10.24,0.00,4600
UUKJL,0.85,-15.00,200

Note that there is no need to use cat | sort here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.