Thanks for your help. While I must admit I didn't quite understand the final
form you gave, that comes from me being rather dumb both in awk/gawk and RE.
I will read the docs and try to work it out myself rather than just pasting
your answer into my script.

I did understood tough the great idea of generating a sort key with gawk and
prepending it to the line to be sorted. I can possibly strip it out easily
by inserting some control caracters after it- in case I don't understand
what you did in the second gawk. :^)

I intend to use this script as a "cd" replacement: in case the dir you typed
doesn't exist, it tries to match it by doing a find/grep. There will be
options for stating the initial search dir, and choosing the
innermost/outermost dir- that's why I need to sort it that way.
I think this script will be very useful for people like me which just don't
remember the right path of  a dir 5 levels below under their ~, and whether
it is mydocs, My_docs, MY docs or some other awful combination.

I wonder if I'm allowed to publish the script into the list when its ready?
I think it would be useful to many newbies which are trying to get
acquainted with bash.

Oh ,and please don't e-mail me telling me that there is such a script
available. Wait till its complete . :^]

-----Mensagem Original-----
De: "civileme" <[EMAIL PROTECTED]>
Para: "Jeferson Lopes Zacco" <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Enviada em: quarta-feira, 11 de julho de 2001 14:14
Assunto: Re: [newbie] sorting a file/list in a special way


> On Tuesday 10 July 2001 20:19, Jeferson Lopes Zacco wrote:
> > Hi all,
> > I need some help in a script I'm  writing.
> > Basically I need to know if there's a way to sort a list in the
following
> > way:
> >
> > Unsorted form:
> > /a/aa/aaaa/aaaaa/aaaaaa
> > /b/bb/bbb/bbbb
> > /d/dd
> > /a
> > /x/x
> > SORTED form:
> > /a
> > /x/x
> > /d/dd
> > /b/bb/bbb/bbbb
> > /a/aa/aaaa/aaaaa/aaaaaa
> >
> > What I want here is to sort the list by the number of "/"s in it, and
> > secondly by its size. I tried sort, tsort and tpx but they don't seem to
be
> > up to this. Will sed or awk handle it? I know nothing about them, but if
> > someone say there's a way I'll find out through the man pages/info.
> >
> > I know I can make a sort function inside my script myself, but I would
have
> > to use arrays and I want it to be bash1 compatible.
> > Any guesses?
> >
> >  --Jeferson L. Zacco aka Wooky
>
>
> Nice question!
>
> Well gawk can count the /'s pretty easily
>
> echo /a/aa/aaa/bbb/cc  |gawk -F/ '{print NF-1}'
> Now length of a line is  length($0) and the line is $0, so
>
> echo /a/aa/aaa/bbb/cc |gawk -F/ '{ print ((NF-1)*1000+length()) $0}'
> will produce
> 5016/a/aa/aaa/bbb/cc
>
> So if you use THAT gawk filter to make a temporary file or pipe you can
sort
> on it and as long as no lines are longer than 1000 characters, you have a
> unique sort key in the beginning of the line--but wait, it is variable in
> length... Pshaw!
>
>  |gawk -F/ '{ printf ("%8d.%s\n", ((NF-1)*1000+length()), $0)}'|
>
> a print from your file to sort on the left of the pipe and a sort on the
> right for the first 8 columns solves the problem.  Then you can use sed or
> gawk to strip off those characters, replacing the . in the first gawk
> statement by something that will NOT be in your string then gawk -F.
'{print
> $1}'  (replacing that . by the delimiter you want as well) after the sort.
>
> Thanks for an interesting one!
>
> Civileme


Reply via email to