[net.unix-wizards] sort problems

naiman@pegasus.UUCP (Ephrayim J. Naiman) (01/20/85)

<munch, munch>

Okay you sort experts why do these two sorts not work ?

sort << !
 Abe
Aba
!

sort << !
Abe
$Aba
!

The first one with no matter what combination of sort options I use
I can't Aba to come out first (except of course -r).
The second one, the '$' seems to screw something up and
it comes back with that line empty.

		Any ideas ?

Thanx

-- 
==> Ephrayim J. Naiman @ AT&T Information Systems Laboratories (201) 576-6259
Paths: [ihnp4, allegra, ahuta, maxvax, cbosgd, lzmi, ...]!pegasus!naiman

mzal@pegasus.UUCP (Mike Zaleski) (01/21/85)

   From: naiman@pegasus.UUCP (Ephrayim J. Naiman)
   Okay you sort experts why do these two sorts not work ?

   sort << !
    Abe
   Aba
   !

This output is correct.  Space is less than 'A' in the ASCII
sequence, which is how sort sorts by default.  The -b option
should cause leading blanks to be ignored.  When I tried it with
this input, it did not work.

   sort << !
   Abe
   $Aba
   !

Try escaping the $ with a backslash when entering this to the shell.
By the way, the ASCII value of '$' is also less than that of 'A'.

-- Mike^Z   [allegra!, ihnp4!] pegasus!mzal   Zaleski@Rutgers

avolio@grendel.UUCP (Frederick M. Avolio) (01/21/85)

> Okay you sort experts why do these two sorts not work ?
> sort << !
>  Abe
> Aba
> !
> 
> sort << !
> Abe
> $Aba
> !
> 

Well, they do work. " Abe" *is* less than "Aba".  Blank is 040  and  A
is 0101.

In the second example the shell is reading the input and passing it to
sort.  $Aba  is interpreted as a shell variable, which probably had no
value.  Hence, the line is blank. (In fact, the C shell tosses it  out
immediately as undefined.  The Bournse shell doesn't care.)
-- 
Fred Avolio
301/731-4100 x4227
UUCP:  {seismo,decvax}!grendel!avolio
ARPA:  grendel!avolio@seismo.ARPA

ka@hou3c.UUCP (Kenneth Almquist) (01/22/85)

> Okay you sort experts why do these two sorts not work ?
> 
> sort << !
>  Abe
> Aba
> !
>
> With no matter what combination of sort options I use
> I can't Aba to come out first (except of course -r).

Try "sort -b +0".  The +0 causes it to sort on the first field, and
the -b causes it to ignore leading blanks in determining the start
of the first field.

> sort << !
> Abe
> $Aba
> !
> 
> The '$' seems to screw something up and it comes back with that line empty.

This has nothing to do with sort.  The shell will normally expand
shell variables inside text included by "<<".  Since $Aba has not
been set, it is replaced with the null string.  To suppress this
behavior, substitute "<<\!" for "<<!".  See sh(1) for more details.
					Kenneth Almquist

ken@turtlevax.UUCP (Ken Turkowski) (01/22/85)

In article <2041@pegasus.UUCP> mzal@pegasus.UUCP (Mike Zaleski) writes:
>
>   From: naiman@pegasus.UUCP (Ephrayim J. Naiman)
>   Okay you sort experts why do these two sorts not work ?
>
>   ...
>
>   sort << !
>   Abe
>   $Aba
>   !
>
>Try escaping the $ with a backslash when entering this to the shell.
>By the way, the ASCII value of '$' is also less than that of 'A'.
>
>-- Mike^Z   [allegra!, ihnp4!] pegasus!mzal   Zaleski@Rutgers

Even better, put quotes around the !:

sort << '!'
Abe
$Aba
!

Which produces the output:

$Aba
Abe

To quote from the Bourne shell documentation:

<< word
	The shell is read up to a line the same as word, or end of
	file.  The resulting document becomes the standard input.  If
	any character of word is quoted, no interpretation is placed
	upon the characters of the document; otherwise parameter and
	command substitution occurs, \newline is ignores, and \ is used
	to quote the characters \ $ ' and the first character of word.

The C shell has analogous propeties.
-- 

Ken Turkowski @ CADLINC, Menlo Park, CA
UUCP: {amd,decwrl,nsc,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken@DECWRL.ARPA

daved@physiol.oz (A/Prof. David F. Davey) (01/24/85)

    Okay you sort experts why do these two sorts not work ?
    
    sort << !
     Abe
    Aba
    !
You must say skip leading blanks on the first column (+0 to right)
sort +0b <<
 Abe
Aba
!

    
    sort << !
    Abe
    $Aba
    !
The shell variable $Aba is most likely undefined. "set -x" before the sort
of the here document would show a blank line input in this case. You can
also see what is happening with:

Aba="Aba from shell variable"
sort << !
Abe
$Aba
!

rwl@uvacs.UUCP (Ray Lubinsky) (01/28/85)

------------------------------------------------------------------------------

> sort << !
>  Abe
> Aba
> !

   The explanation for the first is simple: sort(1) compares the ASCII values
of the first character of each line.  The first character of the line " Abe" is
the space character (octal 040), whereas the first character of the line "Aba"
is 'A' (octal 101).  The lines are sorted in ascending numerical order, hence
"Aba" comes out second, since 040 < 101.

> 
> sort << !
> Abe
> $Aba
> !
> 

   This is a little more obscure.  If you had just done this from standard
input (just typing "sort", then entering lines until you enter the end-of-file
character) you would have found the "$Aba" line would have come first (octal
045 comes before octal 101).  Unfortunately, the way you did it left each line
open to interpretation by the Bourne shell, which thought that "$Aba" was a
shell variable and replaced the line with its value (nothing).

   If you really need to alphabetically sort lines which may be preceded by
tabs or spaces, use the stream editor (sed(1)) to modify the lines on the
fly (eg):  sed 's/^[ @]*//' | sort  (where '@' should be replaced by your
system's tab character).

------------------------------------------------------------------------------

Ray Lubinsky		     University of Virginia, Dept. of Computer Science
			     uucp: decvax!mcnc!ncsu!uvacs!rwl