How to sort after occurence of the / character in a text file Linux console

2

Say that I had a list of entries in a text file:

  • Apple/bcd
  • Pear/abc
  • banana/def
  • orange/cde

and I wanted to sort these entries in the console by what comes after the "/" so they were:

  • Pear/abc
  • Apple/bcd
  • orange/cde
  • banana/def

How do I use the sort function to sort this way without having columns there?
And by only using the sort function, no piping other commands?
I can only sort by the word before the "/".

Euan

Posted 2018-11-01T20:55:21.313

Reputation: 21

Answers

1

Try this:

sort -t / -k 2,2 _file_

The -t / says the field separator is the / character. The -k 2,2 says to sort on field 2, which would be the characters after the first /.

Lewis M

Posted 2018-11-01T20:55:21.313

Reputation: 327