[comp.unix.questions] sed expressions

bguthy@engin.umich.edu (bala s guthy ) (03/27/91)

Can some kind soul explain why the following command prints the first
and the last columns of the ls -l command.



% ls -l | sed 's/[1-9].* //'
% drwx------  xy1991


I can see why it prints the first field but not why it prints the last one.

Thank you.

fwp1@CC.MsState.Edu (Frank Peters) (03/27/91)

: On 27 Mar 91 03:29:40 GMT, bguthy@engin.umich.edu (bala s guthy ) said:

> % ls -l | sed 's/[1-9].* //'
> % drwx------  xy1991

The [1-9] matches any digit between one and nine.  This anchors
beginning of the pattern at the link count field since that is the
first digit in a ls -l line.

The .* matches 0 or more occurences of any character.  This would
ordinarily remove everything up until the end of the line.

But you tossed a space in there.  This means that the pattern will end
with a space.

Now, here is where I expect that you're being confused.  The pattern
matching takes up as much of the line as it can rather than as little.
In other words, it doesn't stop the first time it matches the pattern
on a line.  Rather, it continues parsing to see if it can find another
match using more of the line.

The effect of this is that your pattern stops on the LAST space in the
line instead of the first as I think you expected.  Since the last
space is usually the one before the file name (doesn't have to be...I
have several files with spaces in their names) that is where the
pattern ends and is thus what is removed.

Please let me know if this doesn't make any sense.  I keep thinking I
should be able to explain it more clearly.

Regards,
Fwp
--
Frank Peters   Internet:  fwp1@CC.MsState.Edu         Bitnet:  FWP1@MsState
               Phone:     (601)325-2942               FAX:     (601)325-8921