Sorting using filename of a path

Hi all! i have a question how do you sort the filename of a path directory according to alphabetic order.

Example: sort according to highlighted text. There maybe space for filename

Path=/home/pikamon/Desktop/ABC;
Path=/home/pikamon/Desktop/ABD;
Path=/home/pikamon/Desktop/Riduan la;
Path=/home/pikamon/Desktop/SAMMY;
Path=/home/pikamon/Desktop/.Testing;
Path=/home/pikamon/Desktop/Tom;
Path=/home/pikamon/Desktop/Virus Here;
Path=/opt/pikamon/etc/apps/pikamon/local/inputs.conf~;
Path=/opt/pikamon/etc/system/local/outputs.conf;

All help would be appreciated :slight_smile:

It's not the prettiest solution but it works:

awk -F/ '{a[$NF]=$0}END{for (i in a) print i,a}' inputfile | sort | cut -d\; -f2

or

sed 's#.*/\(.*\)#\1 &#' inputfile | sort | cut -d\; -f2

Alternatively you can use the asort() function in awk if it's supported

1 Like

thanks it work but it leave a blankspace at the front of the text. But it ok

EDIT:
Did not fix the space.
Here is another version that should work.

awk -F/ '{print $NF"�"$0}' file | sort | awk -F� '{print $2}'
Path=/home/pikamon/Desktop/ABC;
Path=/home/pikamon/Desktop/ABD;
Path=/opt/pikamon/etc/apps/pikamon/local/inputs.conf~;
Path=/opt/pikamon/etc/system/local/outputs.conf;
Path=/home/pikamon/Desktop/Riduan la;
Path=/home/pikamon/Desktop/SAMMY;
Path=/home/pikamon/Desktop/.Testing;
Path=/home/pikamon/Desktop/Tom;
Path=/home/pikamon/Desktop/Virus Here;
1 Like

Please be aware that - as � is a multibyte char - not all awk versions/implementations will be able to cope with it.

It can be any character as long as its not in the text and a control character.
What about , is it better then