Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting a list of filenames 1

Status
Not open for further replies.
Apr 6, 2001
1,052
CA
I have a list of filenames in the form x.y
and i want to sort on the last three characters of the field x. Unfortunately x can be variable length.
Any ideas....

thx
stan hubble

I think there is a way to do this in awk but i stayed up to late last night and my brain is foggy.
 
sed -e 's/\(...\.\)/ \1 ' file | sort +1 | sed -e 's/ //g'

There are spaces around the \1 but they may not showup too clearly in the post.

this puts a space around the sort key, sorts on it and then removes the spaces.

however this assumes there are

only the 1 '.'
no spaces
at least 3 characters in the x portion before the '.'.

in the file names.

I am not sure of the SED syntax. the 's/\(...\.\)/ \1 ' comes from VI so I figured it would work in SED as well.

--
 
Thanks tdatgod
it works great (after adding an extra / after the 1)

this is how i am using it

ls | sed -e 's/\(...\.\)/ \1 /' | sort +1 | sed -e 's/ //g'

Also for anyone else that can use it you can increase the size of the sort key (ie instead of 3 characters) by changing s/\(...\ to s/\(....\ or more.

thanks again
stan hubble
 
This solution assumes that files with no characters before dot-extension are to be sorted on the last three characters as well. Otherwise they would have to be taken out of the stream and replaced after sort at some arbitrary location. This script takes the input file as a single argument (i.e. sorton3 file). The script uses sed to grab 3 characters that preceed the extension and places them at the start of the line. If less (1 or 2 only) than three characters preceed then spaces will take the place. Lines without extensions will be treated similarily. It then sorts and removes the first three characters if four or more exist.

You won't have to worry about files with spaces or multiple period characters which would make an awk script a nightmare. I know very little perl but enough to know perl could probably do this better.

You might want to change the sort to sort -n if you have numbers in the 3 characters of interest and need numeric sorting.

What in the world do you need this for? I hope it helps.
Cheers,
ND

#! /bin/sh
sed '
/\(.*\)\(...\)\(\.[^.]*\)$/ {
s//\2\1\2\3/
t
}
/\(.*\)\(..\)\(\.[^.]*\)$/ {
s//\2 \1\2\3/
t
}
/\(.*\)\(.\)\(\.[^.]*\)$/ {
s//\2 \1\2\3/
t
}
/^.....*$/ {
/\(.*\)\(...\)$/ {
s//\2\1\2/
t
}
}' $1 | sort | sed '
/^.....*$/ {
/^.../ s///
}'
# END OF SCRIPT
 
ok i lied its not exactly how i am using it.
I needed this to scan a list of patch files that match a give pattern and return in a specific order.
the filename pattern
$APP_$VER_[optional $SITE]_$BUILD_*.pch
^ - this is where the sequence of 3 characters fit.

Yes I agree perl would probably be the most efficient way but it is not neccessarily installed [yet] on the systems this will run on.

thx again
stan hubble
 
grr i hate proportional spacing....
put the ^ under the *
 
If $SITE is a variable of exactly 3 characters then you can probably simplify all this by preprending $SITE to the string $APP_$VER_[optional $SITE]_$BUILD_*.pch to get

$SITE$APP_$VER_[optional $SITE]_$BUILD_*.pch

once you get your file list or stream do the sort and cleave the first three characters (sed 's/^...//').

Good luck,
ND
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top