bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feature request: counting fields/chars from the right.


From: Bob Proulx
Subject: Re: Feature request: counting fields/chars from the right.
Date: Wed, 13 Sep 2006 10:43:30 -0600
User-agent: Mutt/1.5.9i

Chris Dew wrote:
> Apologies if this is the wrong address, or the wrong protocal for a feature
> request.

You have the right address.

> I need to use cut, but have variable numbers of fields/chars and often need
> to count from the right hand side of a line.

Instead of cut I suggest that you use awk instead.  It is newer,
standard, and has more features than cut.

  echo one two three four | awk '{print$NF}'
  four

  echo one two three four | awk '{print$(NF-1)}'
  three

  echo one two three four | awk '{print $NF, $(NF-1), $1}'
  four three one

> Unfortunately the LIST sytax precludes cut being modified to accept -1 as
> the 1st column on the right and so on (Ruby-style).

Of course you could always use ruby too.  :-)  The shell is not a
contained programming language.  However usually it is good to
restrict the constructs to programs which are covered by standards.
That is why shell programmers prefer the POSIX defined commands such
as awk, grep, sed, etc.  But if you know that you have ruby available
there is nothing saying that you should not use it.

  echo one two three four | ruby -lane 'print $F[0]'
  one

  echo one two three four | ruby -lane 'print $F[-1]'
  four

  echo one two three four | ruby -lane 'print [$F[-1],$F[-2],$F[0]].join(" ")'
  four three one

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]