Operating System - HP-UX
1753771 Members
4810 Online
108799 Solutions
New Discussion юеВ

Re: awk - print range of fields

 
SOLVED
Go to solution
Raynald Boucher
Super Advisor

Re: awk - print range of fields

Hein and James,

Sometimes I'd like to try Perl but We don't use it except in some OS scripts and noboby in my shop is proficient.
It would become a maintenance problem.

Is there a -quick- way to learn Perl?
Which manual would you recommend?

Rayb
James R. Ferguson
Acclaimed Contributor

Re: awk - print range of fields

Hi Raynald:

I'd probably start with "Learning Perl" from the O'Reilly publishers. A compendium of links to good stuff is:

http://perlmonks.org/index.pl?node_id=284175

Like any language, the best way to really learn it is to use it.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: awk - print range of fields

The learning curve for Perl is a bit steeper than with many other scripting languages but the rewards are also greater. It's one of the very few scripting languages that are widely portable. Well-written Perl scripts will run on any flavor of UNIX, Windows, MacOS, VMS, ... without change. Unlike most scripting languages, almost anything that can be done in C can also be done in Perl.

Perhaps the worst quality of Perl is also its best quality: the ability to pack an enormous amount of processing into one line. This characteristic makes it very easy to produce Perl code that is extremely cryptic especially for beginners. It's really quite easy to write Perl in a style that is verbose and clear; it's just not done that often. I suppose that it's one of those "suffering (or at least confusion) is good for the soul" things.
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: awk - print range of fields



Btw... if the contents of field $6 van not match fields $1 - $5, then a simple awk solution could be:

$ awk '{print $4,$2,$3,substr($0,index($0,$6))}' x.tmp

If $6 is a 'short' field then you can increase its uniqueness by pre end-or post pending a knwo separator:

$ awk '{print $4,$2,$3,substr($0,index($0," " $6 " "))}' x.tmp


Cheers,
Hein.
Rodney Hills
Honored Contributor

Re: awk - print range of fields

Maybe a better tools then awk would be "cut".

cut -f 4,2,3,6- inputfile >outputfile

cut assumes tab for the delimiter, but you can override with the -d option.

HTH

Rod Hills
There be dragons...
Dennis Handly
Acclaimed Contributor

Re: awk - print range of fields

>Rod: Maybe a better tools then awk would be "cut".

cut(1) is usually not a better tool than awk.
It has rigorous ideas of delimiters, if you have more than one between fields.
Raynald Boucher
Super Advisor

Re: awk - print range of fields

Thanks again, closing thread.
Raynald Boucher
Super Advisor

Re: awk - print range of fields

Closed!