Re: [OT] GAWK question.

From: ray (ray_at_zianet.com)
Date: 10/21/04


Date: Wed, 20 Oct 2004 20:55:23 -0600

On Wed, 20 Oct 2004 12:52:15 +0000, Mike Cox wrote:

> ray wrote:
>
>> On Wed, 20 Oct 2004 09:59:32 +0000, Mike Cox wrote:
>>
>>> Hi. I have a file that I want to modify with GAWK. It looks like this:
>>>
>>> 235445,53522, 234321,234324,234342
>>> 543444,5345211,3434,6456,2343234
>>> sdfd,sdf33,43343,64364,24355
>>>
>>> And it goes on and on for ages. I want to keep only the first 3 items
>>> seperated by the "," character on each line, and then have everything
>>> deleted that is past the "," third comma.
>>>
>>> So the top example would end up looking like after the gawk operation:
>>>
>>> 235445,53522, 234321
>>> 543444,5345211,3434
>>> sdfd,sdf33,43343
>>>
>>> Now mind you this file goes on for ever,and I don't know how long it is.
>>> What would one write a gawk command to do this?
>>
>> My awk is a little rusty, but with the help of 'man awk' you should be
>> able to figure out: change the field separator to ',' and then print the
>> first three fields.
>
> That doesn't help me much. I'm asking because I'm not very bright and the
> man pages, which I've read, don't help me much. What I'm looking for is an
> answer which I can type into my console and have the work be done
> correctly.
>
> Is there anyone that is bright enough to help me? Or are we all just the
> same stupid 100 IQ points? Maybe there are brighter bulbs in
> comp.os.linux.misc. who can help me.

Put in a file (somename.awk) the following:

BEGIN { FS = "," }
{
  print $1 $2 $3
}

then execute with:

nawk -f somename.awk inputfile >outputfile

You'll learn more if you learn to read man pages.



Relevant Pages