Sorting using filename of a path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting using filename of a path
# 1  
Old 09-24-2013
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

Code:
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 Smilie

Last edited by Scott; 09-24-2013 at 06:19 AM.. Reason: Code tags, please...
# 2  
Old 09-24-2013
It's not the prettiest solution but it works:
Code:
awk -F/ '{a[$NF]=$0}END{for (i in a) print i,a[i]}' inputfile | sort | cut -d\; -f2

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

Alternatively you can use the asort() function in awk if it's supported
This User Gave Thanks to Subbeh For This Post:
# 3  
Old 09-24-2013
Quote:
Originally Posted by Subbeh
It's not the prettiest solution but it works:
Code:
awk -F/ '{a[$NF]=$0}END{for (i in a) print i,a[i]}' inputfile | sort | cut -d\; -f2

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

Alternatively you can use the asort() function in awk if it's supported
thanks it work but it leave a blankspace at the front of the text. But it ok
# 4  
Old 09-24-2013
EDIT:
Did not fix the space.
Here is another version that should work.
Code:
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;


Last edited by Jotne; 09-24-2013 at 08:28 AM..
This User Gave Thanks to Jotne For This Post:
# 5  
Old 09-24-2013
Please be aware that - as § is a multibyte char - not all awk versions/implementations will be able to cope with it.
# 6  
Old 09-24-2013
It can be any character as long as its not in the text and a control character.
What about ¤, is it better then §
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting based on filename

Hello , I have to write a bash script. I will explain the logic based on a scenario. Scenario : Suppose I have few files in a Folder X : FILE_201508.list FILE_201510.list FILE_201507.list abc_201510.csv xyz_201508.csv abc_201507.csv def_201507.csv 1) Now ,... (3 Replies)
Discussion started by: smamrm
3 Replies

2. Shell Programming and Scripting

Sorting a list of filenames but keeping the path information.

Hi All I've googled around for this and can't see a way of doing it. I have a file that contains a number of records that are layed out something like the following. /path/to/directory/that/contains/a/file/I/need/filename.pdf The path itself can vary both in terms of the names and the... (7 Replies)
Discussion started by: Bashingaway
7 Replies

3. Shell Programming and Scripting

"Help" Sorting using filename only without considering the path

Hi, Here's my problem: I have a file "tmp.txt" created by the find command that contain a list of files that meet certain criteria. I need to sort the files using the filenames only not taken into consideration the path: Example for tmp.txt = /tmp/usr/a.txt /tmp/bdir/b.txt Using... (12 Replies)
Discussion started by: snappy46
12 Replies

4. Shell Programming and Scripting

How to remove filename from the path

Hi, I have a list of paths with files at the end. How can strip off filenames. This is what I have: /apps/test/abc/file.txt /apps/new/home/daily/report.xml /apps/old/home/weekly/out/test.sh This is what I need: /apps/test/abc/ /apps/new/home/daily/ /apps/old/home/weekly/out/ ... (10 Replies)
Discussion started by: djanu
10 Replies

5. Shell Programming and Scripting

Split Filename from Absolute Path

Hello, I have the string "/a/b/c/ddd.txt" and i want to get only the filename, in this case "ddd.txt". I have as something known in the script the pattern "/a/b/c/", so I`ve tried something like: echo "/a/b/c/ddd.txt" | cut -d "/a/b/c/" -f2 but it doesn`t go, any help?. thanks, bye (2 Replies)
Discussion started by: rubber08
2 Replies

6. Shell Programming and Scripting

Getting filename with entire path without extension

Hi Experts, need one help.. m writing a shell script for which i need the entire path of the file but without its extension. running the below script gives error at the statement DIR = `dirname $FILE` --command not found. #!/bin/bash jar xvf *jar for FILE in `find . -name "*.class"` ... (3 Replies)
Discussion started by: amicableperson
3 Replies

7. Shell Programming and Scripting

Sorting by Full directory path

I have a text file with full list of files with their full path. I wanted to sort it by directory then files then subdirectory by alphabetically. When I used the sort command it doesn't give like what I want. Could somebody help me on this. Here is the ex: This is what I'm getting... (2 Replies)
Discussion started by: javidraaj
2 Replies

8. Shell Programming and Scripting

fnsplit, Extract filename from path

Hi all, I know this has been covered a lot, I have been searching and reading for hours on the subject, however so far I have been unsuccessful at accomplishing the goal using sed. I know this can be done with parameter expansion (Thanks cfajohnson for a great explanation of parameter... (5 Replies)
Discussion started by: Festus Hagen
5 Replies

9. Shell Programming and Scripting

to check whether a directory or filename path is valid or not

the script on excution should take a directory path from useran a numric input and it should check indicate whether its write or not? if the cmmd sh<script-name>,dir/path.<500>" is greater than 500 in size should be copied to dir ,temp in pwd and display the mesage'files of 2000 bytes hav been... (4 Replies)
Discussion started by: arukr
4 Replies

10. UNIX for Dummies Questions & Answers

Remove path from filename

In a foreach loop I end up with $file containing the filename INCLUDING the whole path. I want this reduced to just the filename, but I can't seem to remember how I did it some years back. I am sure I can do it with "sed", but I am pretty sure I have seen a simpler command. Anyone? borgeh (3 Replies)
Discussion started by: borgeh
3 Replies
Login or Register to Ask a Question