"Help" Sorting using filename only without considering the path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "Help" Sorting using filename only without considering the path
# 1  
Old 05-11-2011
[SOLVED]"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 =
Code:
/tmp/usr/a.txt
/tmp/bdir/b.txt

Using this:
Code:
cat tmp.txt | sort > sorted.txt

I get sorted.txt =
Code:
/tmp/bdir/b.txt
/tmp/usr/a.txt

What I want is to only consider the filename not the path and to give:
Code:
/tmp/usr/a.txt
/tmp/bdir/b.txt

Since a.txt is before b.txt

I try using basename but the path which is still needed get strip off:
Code:
basename`cat tmp.txt' |sort > sorted.txt

Any ideas???

I am sure this is probably easy to achieve but I am kind of a newbie here.

Thank you.

Last edited by snappy46; 05-18-2011 at 12:02 PM.. Reason: Please use code tags
# 2  
Old 05-11-2011
One idea is to prefix the long filename with the basename version.
For example (in ksh).

Code:
#!/bin/ksh
cat tmp.txt | while read filename
do
        filename2=`basename "${filename}"`
        echo "${filename2}:${filename}"
done | sort | awk -F: '{print $2}'

What Shell are you using?
This User Gave Thanks to methyl For This Post:
# 3  
Old 05-11-2011
Quote:
Originally Posted by methyl
What Shell are you using?
I guess I should have mentioned that in my original post. The shell (sh) is from an outdated busybox on my media player. Sorry I can not be more specific on the busybox version since I do not have access to my player right now. Unfortunately awk is not available on all players so I would rather not use it if possible.

Thanks for your input and quick reply.

Snappy46
# 4  
Old 05-11-2011
Code:
sed 's:.*/:& :' tmp.txt | sort -k 2 | sed 's: ::'

Or... if all your file are at the same depth level (here : under the third "/" so level 4) you can even shorten this way :

Code:
sort -t "/" -k 4 <tmp.txt

just man sort

Example:
Code:
# cat mylist
/oracle/SID/sapreorg
/oracle/SID/sapdata1
/oracle/SID/sapdata2
/oracle/SI8/sapdata3
/oracle/SI9/sapdata4
/oracle/SI7/saptrace
/oracle/SI6/saparch
/oracle/SI5/sapcheck
/oracle/SI4/sapbackup
/oracle/SI3/sapdata5
/oracle/SI2/sapdata6
/oracle/SI1/sapdata7

Code:
# sort -t "/" -k4 <mylist
/oracle/SI6/saparch
/oracle/SI4/sapbackup
/oracle/SI5/sapcheck
/oracle/SID/sapdata1
/oracle/SID/sapdata2
/oracle/SI8/sapdata3
/oracle/SI9/sapdata4
/oracle/SI3/sapdata5
/oracle/SI2/sapdata6
/oracle/SI1/sapdata7
/oracle/SID/sapreorg
/oracle/SI7/saptrace


Last edited by ctsgnb; 05-11-2011 at 02:18 PM..
# 5  
Old 05-11-2011
As this is Busybox I must sign off now.
# 6  
Old 05-11-2011
Quote:
Originally Posted by ctsgnb
Code:
sed 's:.*/:& :' tmp.txt | sort -k 2 | sed 's: ::'

Or... if all your file are at the same depth level (here : under the third "/" so level 4) you can even shorten this way :

Code:
sort -t "/" -k 4 <tmp.txt

just man sort
ctsgnb,

My result on my media player are somewhat different:

Code:
/tmp # cat mylist 
/oracle/SID/sapreorg
/oracle/SID/sapdata1
/oracle/SID/sapdata2
/oracle/SI8/sapdata3
/oracle/SI9/sapdata4
/oracle/SI7/saptrace
/oracle/SI6/saparch
/oracle/SI5/sapcheck
/oracle/SI4/sapbackup
/oracle/SI3/sapdata5
/oracle/SI2/sapdata6
/oracle/SI1/sapdata7
/tmp # sed 's:.*/:& :' mylist | sort -k 2 | sed 's: ::'
/oracle/SI1/sapdata7
/oracle/SI2/sapdata6
/oracle/SI3/sapdata5
/oracle/SI4/sapbackup
/oracle/SI5/sapcheck
/oracle/SI6/saparch
/oracle/SI7/saptrace
/oracle/SI8/sapdata3
/oracle/SI9/sapdata4
/oracle/SID/sapdata1
/oracle/SID/sapdata2
/oracle/SID/sapreorg
/tmp # sort -t "/" -k 4 <mylist 
/oracle/SI1/sapdata7
/oracle/SI2/sapdata6
/oracle/SI3/sapdata5
/oracle/SI4/sapbackup
/oracle/SI5/sapcheck
/oracle/SI6/saparch
/oracle/SI7/saptrace
/oracle/SI8/sapdata3
/oracle/SI9/sapdata4
/oracle/SID/sapdata1
/oracle/SID/sapdata2
/oracle/SID/sapreorg
/tmp #

The sort command on busybox 1.1.3 is quite primitive and does not support the -k flag;

/tmp # sort -h
sort: illegal option -- h
BusyBox v1.1.3 (2011.02.16-01:55+0000) multi-call binary

Usage: sort [-nru] [FILE]...

Sorts lines of text in the specified files

Options:
-n sort numbers
-r reverse sort order
-u suppress duplicate lines

Thanks a lot for your help but it looks like I will have to try a different way.

---------- Post updated at 08:43 PM ---------- Previous update was at 07:07 PM ----------

Quote:
Originally Posted by methyl
As this is Busybox I must sign off now.
Why going so fast Smilie Actually a small change in your script did the trick for me without using awk. Here the code that I used and it worked perfectly:

Code:
#!/bin/sh
while read filename
do
        filename2=`basename "${filename}"`
        echo "${filename2}:${filename}"
done < mylist | sort | cut -d: -f2

Remove the unneeded cat command; no real need to start another process when redirecting to the loop does the job and replace awk by the cut command.

Thank you very much to both of you for your inputs; I just needed a little push in the right direction which you both provided.
# 7  
Old 05-12-2011
ok it looks like your sort doesn't have the -k option nor the -t option ...
so try this :

Code:
sed 's:\(.*/\)\(.*\):\2 \1\2:' mylist.txt | sort | sed 's:.* ::'

or this
Code:
sed 's:\(.*/\)\([^/]*\):\2 \1\2:' mylist.txt | sort | sed 's:[^/]* ::'


Last edited by ctsgnb; 05-12-2011 at 04:41 AM..
This User Gave Thanks to ctsgnb For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. Shell Programming and Scripting

find . -path "*_nobackup*" -prune -iname "*.PDF" \( ! -name "*_nobackup.*" \)

These three finds worked as expected: $ find . -iname "*.PDF" $ find . -iname "*.PDF" \( ! -name "*_nobackup.*" \) $ find . -path "*_nobackup*" -prune -iname "*.PDF" They all returned the match: ./folder/file.pdf :b: This find returned no matches: $ find . -path "*_nobackup*" -prune... (3 Replies)
Discussion started by: wolfv
3 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

6. UNIX for Dummies Questions & Answers

"tail -n 1 filename" error while "head -n 1 filename" is ok?

Hi all, I was wondering why tail -n 2 filename produce an error when I manage to do similar command on head -n 2 filename SunOS{type8code0}: tail -n 2 filename usage: tail ] tail ] (2 Replies)
Discussion started by: type8code0
2 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question