List Info

Thread: How to extract *and reorder* columns?




How to extract *and reorder* columns?
country flaguser name
United States
2008-03-01 17:13:22


I need to extract and reorder some columns from some very
large
files.  GNU cut will extract the columns nicely and quickly,
but
won't reorder them.  I can do the reordering easily by
piping the
output of cut through a perl one-liner, but I'm looking for
something
faster.

Is there a GNU utility that will do this?

TIA!

Kynn
-- 
NOTE: In my address everything before the first period is
backwards;
and the last period, and everything after it, should be
discarded.

Re: How to extract *and reorder* columns?
user name
2008-03-01 17:52:44
kj wrote:
> I need to extract and reorder some columns from some
very large
> files.  GNU cut will extract the columns nicely and
quickly, but
> won't reorder them.  I can do the reordering easily by
piping the
> output of cut through a perl one-liner, but I'm looking
for something
> faster.
> 
> Is there a GNU utility that will do this?

Not just GNU but POSIX standard.  Use awk.

  echo one two three four | awk '{print $3, $2, $1, $4}'
  three two one four

NF is the number of fields on the line.  Therefore $NF is
the last
field on the line.  This is a common idiom.

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

Therefore $(NF-1) is the next from last and so forth.  This
allows
selecting from the end of the line.

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

Bob



Re: How to extract *and reorder* columns?
country flaguser name
United States
2008-03-02 11:16:40
In <mailman.8151.1204415571.18990.help-gnu-utilsgnu.org> bobproulx.com (Bob Proulx) writes:

>kj wrote:
>> I need to extract and reorder some columns from
some very large
>> files.  GNU cut will extract the columns nicely and
quickly, but
>> won't reorder them.  I can do the reordering easily
by piping the
>> output of cut through a perl one-liner, but I'm
looking for something
>> faster.
>> 
>> Is there a GNU utility that will do this?

>Not just GNU but POSIX standard.  Use awk.

This is quite a revelation.  I never knew that awk was so
much
faster than perl.  I'm getting speed-ups of 6 or 7-fold...

Thanks!

kynn

-- 
NOTE: In my address everything before the first period is
backwards;
and the last period, and everything after it, should be
discarded.

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )