awk script

awk script

Post by Bill Piet » Thu, 20 Dec 2001 10:23:41



Each day I have a file generated with several hundred lines. I'd like to
create a single awk script that would print to screen only the line(s)
with the most fields. I've futzed around with this and come to a dead
end, brain-dead probably. Found lots of interesting examples that help
not. O'Reilly's sed & awk book hasn't provided the inspiration I'd hoped
for. A nudge in the right direction sure would be appreciated.

Bill Piety
Registered Linux user #193275

 
 
 

awk script

Post by Sean Russel » Fri, 21 Dec 2001 02:08:34



> Each day I have a file generated with several hundred lines. I'd like to
> create a single awk script that would print to screen only the line(s)
> with the most fields. I've futzed around with this and come to a dead
> end, brain-dead probably. Found lots of interesting examples that help
> not. O'Reilly's sed & awk book hasn't provided the inspiration I'd hoped
> for. A nudge in the right direction sure would be appreciated.

Are you specifically wanting to use AWK for any reason, and not another
scripting language?  Are you looking for a command-line script, or an
executable script?  When you say "the most fields", do you mean the most
fields for that file, or do you know how many fields "the most fields" is?

If the maximum number of fields is variable, you'll have to parse the file
twice; the first time to determine the max value, and the second time to
print the lines that have that many fields.  The good news is that "several
hundred lines" shouldn't be very time consuming to parse twice.

I'm not an AWK guru, but the following script should work:

  {
    max = 0
    while (getline < ARGV[1])
      if (NF > max) max = NF
    while (getline)
      if (NF == max) print $0
  }

Save this to a file called maxlines.awk and run:

  awk -f maxlines.awk myfile

(where "myfile" is your log file)

--
--- SER

 
 
 

awk script

Post by Bill Piet » Sun, 30 Dec 2001 01:50:11




>> Each day I have a file generated with several hundred lines. I'd like
>> to create a single awk script that would print to screen only the
>> line(s) with the most fields. I've futzed around with this and come to
>> a dead end, brain-dead probably. Found lots of interesting examples
>> that help not. O'Reilly's sed & awk book hasn't provided the
>> inspiration I'd hoped for. A nudge in the right direction sure would be
>> appreciated.

> Are you specifically wanting to use AWK for any reason, and not another
> scripting language?  Are you looking for a command-line script, or an
> executable script?  When you say "the most fields", do you mean the most
> fields for that file, or do you know how many fields "the most fields"
> is?

> If the maximum number of fields is variable, you'll have to parse the
> file twice; the first time to determine the max value, and the second
> time to print the lines that have that many fields.  The good news is
> that "several hundred lines" shouldn't be very time consuming to parse
> twice.

> I'm not an AWK guru, but the following script should work:

>   {
>     max = 0
>     while (getline < ARGV[1])
>       if (NF > max) max = NF
>     while (getline)
>       if (NF == max) print $0
>   }

> Save this to a file called maxlines.awk and run:

>   awk -f maxlines.awk myfile

> (where "myfile" is your log file)

This works to a "T". Thanks very much for your help. My 'awking' is
rudimentary and while this works, I've still got to dissect & figure out
why it works. Happy New Year, all.
--
Bill Piety
Registered Linux user #193275
 
 
 

1. Need help with simple awk script

Hello,

I am not much of a UNIX shell programmer (I am learning quickly
though), but I am trying to figure out a way to get awk (or other
utility) to parse thru a space delimited text file and send the
information the BSDI v3.0 adduser program.

The text file is actually a subset of a Linux password file that I
created using awk.  We are migrating from Linux to BSDI v3.0 and
don't want to add the users to the new machine by hand.  Therefore I
thought I would dump only the information from the Linux passwd file
that was necessary and feed it to the BSDI adduser program (with a few
switches).

The text file has around 500 lines in the format of

{username} {DES encrypted password} {First Name} {Last Name} {shell}

                (there is a space between each field)

The BSDI "addsuser" command has a whole bunch of switches, therefore I
will need to make the AWK script add users to the BSDI machine by
issuing these switches as well as the account information for each
user.

The AWK script will need to send the following info to adduser for
each line in the space delimited text file...:

adduser -g {groupname} -h {base home directory} -H {user's home
directory} -p {DES encrypted password} -s {shell}

I don't necessarily need someone to write the whole darn thing for me
(although that would be great), just knowing how to approach the task
and maybe giving me a general "outline" for the script would be a huge
help!!

Thanks!

2. X on 310ED Thinkpad install

3. awk script that works like "cut"

4. Update openssh (Newbie)

5. getopts / awk script

6. make buildworld;make installworld

7. awk scripting

8. SCSI Sun Ultra 5

9. awk script fails on solaris 2.4

10. 'getline < "-" in awk script does not work!

11. Can you launch an awk script this way?

12. awk script

13. Help with awk script