i've tried to sort fixed length records but it aint working

i've tried to sort fixed length records but it aint working

Post by Raymond Michael Mor » Sat, 23 Oct 1999 04:00:00



consider my directories:

drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
drwx------   2 user10   esu         8192 Dec  1  1998 News/
drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/

-rw-------   1 user10   esu          648 Oct  4 17:55 joy
-rw-------   1 user10   esu            0 Oct 22 15:02 l
drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:

drwx------   2 user10   esu         8192 Dec  1  1998 News/
drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

-rw-------   1 user10   esu            0 Oct 22 15:02 l
-rw-------   1 user10   esu          648 Oct  4 17:55 joy

when I expect this:

drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
drwx------   2 user10   esu         8192 Dec  1  1998 News/
drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
-rw-------   1 user10   esu          648 Oct  4 17:55 joy
-rw-------   1 user10   esu            0 Oct 22 15:02 l

what am I dowing wrong?

--

---- [ o_[ o [ --[   [ o [ V [ U [ \|[ \|[ [ [ [ --- Raymond Moran (Ringo) ----
---- [ [ [ [ [-- [[ [[ | [ M [_ _[|\ [|\ [-[-[-[ ------------------------------
-- The only computer geek who can add 1 + 1 and will always come up with 10. --

 
 
 

i've tried to sort fixed length records but it aint working

Post by Al Shark » Sat, 23 Oct 1999 04:00:00



> if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:
> drwx------   2 user10   esu         8192 Dec  1  1998 News/
> drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
> drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
> drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
> drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

> -rw-------   1 user10   esu            0 Oct 22 15:02 l
> -rw-------   1 user10   esu          648 Oct  4 17:55 joy
> when I expect this:
> drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
> drwx------   2 user10   esu         8192 Dec  1  1998 News/
> drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
> drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
> drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
> -rw-------   1 user10   esu          648 Oct  4 17:55 joy
> -rw-------   1 user10   esu            0 Oct 22 15:02 l

What you are probably looking for is a second key:

sort -k1.1d,1.2d -k1.55

although I don't know why you expect the symbolic link to go away.

 
 
 

i've tried to sort fixed length records but it aint working

Post by Barry Margoli » Sat, 23 Oct 1999 04:00:00




>consider my directories:

>drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
>drwx------   2 user10   esu         8192 Dec  1  1998 News/
>drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/

>-rw-------   1 user10   esu          648 Oct  4 17:55 joy
>-rw-------   1 user10   esu            0 Oct 22 15:02 l
>drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
>drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

>if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:

>drwx------   2 user10   esu         8192 Dec  1  1998 News/
>drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
>drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
>drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
>drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

>-rw-------   1 user10   esu            0 Oct 22 15:02 l
>-rw-------   1 user10   esu          648 Oct  4 17:55 joy

>when I expect this:

>drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
>drwx------   2 user10   esu         8192 Dec  1  1998 News/
>drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
>drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
>drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
>-rw-------   1 user10   esu          648 Oct  4 17:55 joy
>-rw-------   1 user10   esu            0 Oct 22 15:02 l

>what am I dowing wrong?

Why do you expect sort to remove the line that begins with "l"?

Other than that, I don't see any difference in the first two columns
between what you got and what you expect.  Since you're only sorting on the
first two columns, the rest don't have any effect.

It looks like you expect all the directories to have the same order in the
result as they did in the original data.  But the sort command does *not*
implement a stable sort.  So records that have equivalent keys can end up
in any order amongst themselves in the output.

Since ls -l sorts by the filename, you can achieve what you want by using a
second key to do the same sort:

sort -k1.1,1.2d -k 9

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

i've tried to sort fixed length records but it aint working

Post by Christopher J. Matter » Sat, 23 Oct 1999 04:00:00



> consider my directories:
> drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
> drwx------   2 user10   esu         8192 Dec  1  1998 News/
> drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/

> -rw-------   1 user10   esu          648 Oct  4 17:55 joy
> -rw-------   1 user10   esu            0 Oct 22 15:02 l
> drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
> drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
> if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:
> drwx------   2 user10   esu         8192 Dec  1  1998 News/
> drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
> drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
> drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
> drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

> -rw-------   1 user10   esu            0 Oct 22 15:02 l
> -rw-------   1 user10   esu          648 Oct  4 17:55 joy
> when I expect this:
> drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
> drwx------   2 user10   esu         8192 Dec  1  1998 News/
> drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
> drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
> drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
> -rw-------   1 user10   esu          648 Oct  4 17:55 joy
> -rw-------   1 user10   esu            0 Oct 22 15:02 l
> what am I dowing wrong?

I assume you left out the symbolic link in the "what I expect"
example by mistake.

If I understand your problem, you are complaining that sort rearranged
the your records, rather than preserving the original order as much
as possible while performing the sort.  This characteristic is
call "stability", and the simple fact is that the sort program
does not do a stable sort.  Sort will present you your records
sorted by the criteria you specified.  It makes no other guarantees
about how your records will be sequenced.  In this case, since
the original order is the "sorted by filename" order that ls gives
you by default, you could simply say "sort -k1.1d,1.2d,9".  A more
generic solution is to tack on a sequence field, make it your
final sort key, and then strip it off again after the sort.

                            Chris Mattern

 
 
 

i've tried to sort fixed length records but it aint working

Post by John Gord » Sat, 23 Oct 1999 04:00:00



Quote:> if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:

you're telling sort to use a sort key of the first and second characters
of the first field.

> drwx------   2 user10   esu         8192 Dec  1  1998 News/
> drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
> drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
> drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
> drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

> -rw-------   1 user10   esu            0 Oct 22 15:02 l
> -rw-------   1 user10   esu          648 Oct  4 17:55 joy
> when I expect this:
> drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
> drwx------   2 user10   esu         8192 Dec  1  1998 News/
> drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
> drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
> drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
> -rw-------   1 user10   esu          648 Oct  4 17:55 joy
> -rw-------   1 user10   esu            0 Oct 22 15:02 l

it would have been helpful to also provide a verbal explanation of what
you're expecting.  it's hard to divine exactly what you want from this
output.  also, what happened to cs?

i assume, though, you want to sort first by file size in descending order,
and then by filename in ascending order.  try this:

  ls -l | sort -k 5,5nr -k 9,9d

---
John Gordon                  "No Silicon Heaven?  Preposterous!  Where would

 
 
 

i've tried to sort fixed length records but it aint working

Post by Raymond Michael Mor » Thu, 28 Oct 1999 04:00:00


you were right about the symbolic link.  I had accidently omitted that in my expected output.  The whole purpose of this exercise is to sort by directory, then by filename (the only sane remnant of MS OS's).  I assume you that when you suggested "sort -k1.1d,1.2d,9" you meant "sort -k1.1d,1.2d -k9".  But even that doens't yield desireable results.

btw, here is the corrected desireable result:

drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
drwx------   2 user10   esu         8192 Dec  1  1998 News/
drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

-rw-------   1 user10   esu          648 Oct  4 17:55 joy
-rw-------   1 user10   esu            0 Oct 22 15:02 l

thanks to you (and everyone else who have responded) for providing insight on how sort sorts.



: > consider my directories:

: > drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
: > drwx------   2 user10   esu         8192 Dec  1  1998 News/
: > drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/

: > -rw-------   1 user10   esu          648 Oct  4 17:55 joy
: > -rw-------   1 user10   esu            0 Oct 22 15:02 l
: > drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
: > drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

: > if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:

: > drwx------   2 user10   esu         8192 Dec  1  1998 News/
: > drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
: > drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
: > drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
: > drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

: > -rw-------   1 user10   esu            0 Oct 22 15:02 l
: > -rw-------   1 user10   esu          648 Oct  4 17:55 joy

: > when I expect this:

: > drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
: > drwx------   2 user10   esu         8192 Dec  1  1998 News/
: > drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
: > drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
: > drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
: > -rw-------   1 user10   esu          648 Oct  4 17:55 joy
: > -rw-------   1 user10   esu            0 Oct 22 15:02 l

: > what am I dowing wrong?

: I assume you left out the symbolic link in the "what I expect"
: example by mistake.

: If I understand your problem, you are complaining that sort rearranged
: the your records, rather than preserving the original order as much
: as possible while performing the sort.  This characteristic is
: call "stability", and the simple fact is that the sort program
: does not do a stable sort.  Sort will present you your records
: sorted by the criteria you specified.  It makes no other guarantees
: about how your records will be sequenced.  In this case, since
: the original order is the "sorted by filename" order that ls gives
: you by default, you could simply say "sort -k1.1d,1.2d,9".  A more
: generic solution is to tack on a sequence field, make it your
: final sort key, and then strip it off again after the sort.

:                             Chris Mattern

--

---- [ o_[ o [ --[   [ o [ V [ U [ \|[ \|[ [ [ [ --- Raymond Moran (Ringo) ----
---- [ [ [ [ [-- [[ [[ | [ M [_ _[|\ [|\ [-[-[-[ ------------------------------
-- The only computer geek who can add 1 + 1 and will always come up with 10. --

 
 
 

i've tried to sort fixed length records but it aint working

Post by Raymond Michael Mor » Thu, 28 Oct 1999 04:00:00



: > if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:

: you're telling sort to use a sort key of the first and second characters
: of the first field.

: > drwx------   2 user10   esu         8192 Dec  1  1998 News/
: > drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
: > drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
: > drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
: > drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

: > -rw-------   1 user10   esu            0 Oct 22 15:02 l
: > -rw-------   1 user10   esu          648 Oct  4 17:55 joy

: > when I expect this:

: > drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
: > drwx------   2 user10   esu         8192 Dec  1  1998 News/
: > drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
: > drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
: > drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
: > -rw-------   1 user10   esu          648 Oct  4 17:55 joy
: > -rw-------   1 user10   esu            0 Oct 22 15:02 l

: it would have been helpful to also provide a verbal explanation of what
: you're expecting.  it's hard to divine exactly what you want from this
: output.  also, what happened to cs?

sorry, i want to sort first by directory (or link or whatever), then by file (i omitted the -k9).  I accidently omitted the link and it should be in there after all the directories.

: i assume, though, you want to sort first by file size in descending order,
: and then by filename in ascending order.  try this:

:   ls -l | sort -k 5,5nr -k 9,9d

: ---
: John Gordon                  "No Silicon Heaven?  Preposterous!  Where would

--

---- [ o_[ o [ --[   [ o [ V [ U [ \|[ \|[ [ [ [ --- Raymond Moran (Ringo) ----
---- [ [ [ [ [-- [[ [[ | [ M [_ _[|\ [|\ [-[-[-[ ------------------------------
-- The only computer geek who can add 1 + 1 and will always come up with 10. --

 
 
 

i've tried to sort fixed length records but it aint working

Post by Raymond Michael Mor » Thu, 28 Oct 1999 04:00:00



: >
: > if i apply: "% ls -l | sort -k1.1d,1.2d", i get this:
: > drwx------   2 user10   esu         8192 Dec  1  1998 News/
: > drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
: > drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
: > drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
: > drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/

: > -rw-------   1 user10   esu            0 Oct 22 15:02 l
: > -rw-------   1 user10   esu          648 Oct  4 17:55 joy
: > when I expect this:
: > drwx------   4 user10   esu         8192 Oct 22 14:41 Mail/
: > drwx------   2 user10   esu         8192 Dec  1  1998 News/
: > drwxr-xr-x   3 user10   esu         8192 Oct 22 10:48 bin/
: > drwx------   2 user10   esu         8192 Oct 15 15:40 nsmail/
: > drwxr-xr-x   9 user10   esu         8192 Oct 14 14:34 public_html/
: > -rw-------   1 user10   esu          648 Oct  4 17:55 joy
: > -rw-------   1 user10   esu            0 Oct 22 15:02 l

: What you are probably looking for is a second key:

: sort -k1.1d,1.2d -k1.55

: although I don't know why you expect the symbolic link to go away.

THAT'S IT!  but i don't understand the secondary sort of -k1.55 when i would expect -k9.  Are you saying startcol=1, startchar=55th column?

--

---- [ o_[ o [ --[   [ o [ V [ U [ \|[ \|[ [ [ [ --- Raymond Moran (Ringo) ----
---- [ [ [ [ [-- [[ [[ | [ M [_ _[|\ [|\ [-[-[-[ ------------------------------
-- The only computer geek who can add 1 + 1 and will always come up with 10. --

 
 
 

i've tried to sort fixed length records but it aint working

Post by Ken Pizzi » Thu, 28 Oct 1999 04:00:00


On 27 Oct 1999 00:22:25 GMT,

Quote:> The whole purpose of this exercise is to sort
>by directory, then by filename (the only sane remnant of MS OS's). I
>assume you that when you suggested "sort -k1.1d,1.2d,9" you meant
>"sort -k1.1d,1.2d -k9". But even that doens't yield desireable results.

"sort -k1.1d,1.2 -k9" works just fine for me to duplicate your
(updated) "desired results" from your input.  What version of
"sort" are you using?

                --Ken Pizzini

 
 
 

i've tried to sort fixed length records but it aint working

Post by RastaMunn!! » Thu, 28 Oct 1999 04:00:00



> On 27 Oct 1999 00:22:25 GMT,

> > The whole purpose of this exercise is to sort
> >by directory, then by filename (the only sane remnant of MS OS's). I
> >assume you that when you suggested "sort -k1.1d,1.2d,9" you meant
> >"sort -k1.1d,1.2d -k9". But even that doens't yield desireable results.

> "sort -k1.1d,1.2 -k9" works just fine for me to duplicate your
> (updated) "desired results" from your input.  What version of
> "sort" are you using?

>            --Ken Pizzini

i don't know the version but i'll slap myself on the face just finding out
that even -k9 works.  what threw me off was that the primary sort didn't
yield predicted results (i.e. it sorted the first space-delimited column
alphabetically as opposed the first character in that column).  therefore,
i neglected to test the secondary sort until i could fix the primary one.
i don't know what goes on behind the sort algorithm, but here's the
synopsis i've concluded:

sort -k1.1d,1.2d        # does not sort as expected, but with -k9,
sort -k1.1d,1.2d -k9    # it sorts primary as expected as well as the
                        # secondary fields.  weird huh?
thanks for your help.


---- [ o_[ o [ --[   [ o [ V [ U [ \|[ \|[ [ [ [ --- Raymond Moran (Ringo) ----
---- [ [ [ [ [-- [[ [[ | [ M [_ _[|\ [|\ [-[-[-[ ------------------------------
-- The only computer geek who can add 1 + 1 and will always come up with 10. --

 
 
 

i've tried to sort fixed length records but it aint working

Post by Barry Margoli » Thu, 28 Oct 1999 04:00:00




>sort -k1.1d,1.2d    # does not sort as expected, but with -k9,
>sort -k1.1d,1.2d -k9        # it sorts primary as expected as well as the
>                    # secondary fields.  weird huh?

What's so weird?  What precisely is it you're expecting from the first
sort?  As others have posted, sort isn't stable.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

i've tried to sort fixed length records but it aint working

Post by Barry Margoli » Thu, 28 Oct 1999 04:00:00




Quote:>THAT'S IT!  but i don't understand the secondary sort of -k1.55 when i
>would expect -k9.  Are you saying startcol=1, startchar=55th column?

-k1.55 means "55th character starting from the beginning of field 1".  This
happens to be the same place as the start of field 9, so the two are
equivalent ways of saying the same thing.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

1. Fixed length records containing 2 different records types with fixed field widths

What game are you playing? You posted exactly the same question to
comp.lang.awk have got an answer already 20 hours ago to which you
also replied before reposting this again in c.u.s. You haven't even
enhanced your script considering Kenny's response.

2. Why can't I ping my own IP Aliases?

3. sorting fixed length records/fields (large files)

4. help i cant get linux up and running

5. can sh remove \n's from fixed length ascii records?

6. Mandrake and X

7. sort fixed length file

8. Welcome to comp.unix.shell [Frequent posting]

9. If it aint broke, I'll fix it

10. Pad records to fixed length.

11. Editing fixed-length records

12. Editing fixed-length records (fwd)