View Poll Results: do u agree with the new AP feature. (showing AP poster)

Voters
89. You may not vote on this poll
  • YES

    60 67.42%
  • NO

    29 32.58%
Results 1 to 6 of 6

Thread: awk's -INTRO-

  1. #1

    Cool awk's -INTRO-

    If this was posted anywhere on this forum let me know cause I didn't see it anywhere when I search. So I will explore some of the uses of the powerful awk language. Yes, it's a language so may argue since it's can be used on the cmd-line within a Unix console. This tutorial assumes UR familiar with using Unix commands and editors.

    Awk, is a pattern-scanning & processing language. Kewl Huh! It offers power user's a command-line tool to maniplate input. Correct me if I made a mistake, cause I'm no expert. Awk as I understood it is useful for doing interesting things with input. My first experience with Unix I didn't have anuff ram to run the gui. So I was forced to use the console until I built a new box. *"Awk first saw light of day with version 7 UNIX, around 1978."-O'Reilly "sed and awk" . Enough with the boring history for now let explore how we can use it's power.
    type at console prompt:
    Code:
    $ echo -e "AO MEMBERS make a kewl community">AOer's
              $ echo -e "AO MEMBERS are very helpful">>AOer's
              $ echo -e "AO best site on the Web">>AOer's
              $ awk '{ print "AO ADDICTS & SENIOR'S RULE"} AOer's
    output should look as follows:
    AO ADDICTS & SENIOR'S RULE
    AO ADDICTS & SENIOR'S RULE
    AO ADDICTS & SENIOR'S RULE


    The above example as I said before awk reads input. It prints the "AO ADDICTS' & SENIOR'S RULE" for each line of input read from the file "AOer's".

    Now to better understand awk as a programming language. It is helpful to write awk scripts
    this makes it second nature in using it since it is easy to learn Below is a description of awk's three procedures we can use which consists of the 3 routines: BEGIN, main loop, END.

    1st routine-- BEGIN
    #this is done before input is read, this can be useful for all sorts of things
    # use your imagination.

    2nd routine--main loop
    # this is the action to perform on the input, kewl I like

    3rd routine--END
    #this is done after input is read, this is also useful

    Always comment your scripts it make it easier to debug later. Comment with the # symbol they can be placed anywhere within your scripts. Do U understand the above, if not maybe this will help. Type at prompt
    Code:
     $ echo a b c d | awk 'BEGIN {one=1, two =2}
                                                              > {print $(one + two)}'
    c output. This help U understand the laws of using awk. Ok, Ok:-V !! Let's do something more interesting with awk. Let's make a file called "AOTOP10POSTERS"
    I hope AO members aren't upset with me using them as working examples. If U don't know how to create a file from the console do a
    Code:
    $ cat>AOTOP10POSTERS
      Tedobl,2261
      Souleman,2031
      KorpDeath,1774
      Terr,1718
      ac1dap3ctrum,1466
      s0nic,1422
      Ennis,1272
      conf1rm3d k1ll,1168
      valhallen,1149
      victorkaum,1124
    on new line hit ctrl-D or use your favorite editor.

    Now we can work with awk to read the input of the new AO file at the prompt type:
    awk -F, '{printf "%-16c\t %-04d\n", $1, $2}' then enter.
    The '-F, ' tells awk the field separator is a comma. {printf "%-16c\t %-04d\n", $1, $2}' is the action to perform on each line of input read in. "printf" is a C language formatting function. "%" tells how to line up the fields literally. "-" is to left align. The numbers tells how many characters to display in each field. The trailings c's & d's represent c = any character and the d = the field is to be an integer. The \t is a tab & \n is a new line or carriage return.

    Remember the 3 routines of awk's law we can use the BEGIN, main loop, END. Open your favorite editor and write the simple awk script. The BEGIN routine is done before input is read
    Code:
    BEGIN {
                  { FS = "," } 
                      { print "\t\t AO's Top 10 Posters\n" }
                  { "date" | getline d }
                  { printf "\t %s\n", d }
                  { print "Members Name\n" }
                  { print "==========================================\n" }
                  } #this ends the begin routine and makes a header for the input of file
    #now for main loop
                  { printf "%-16c\t %-04d\n", $1,$2 }
    #this is how we want the input formatted
    #beginning the end routine
                END { print " "       #makes a blank line
                           print NR, "records processed."    #shows numbers of input lines read
                           total = $2   #set variable total to second field
                           avg = total/3      #avgrage for top posters base on three posts per day
    # and the number post for that user
                           print $1, avg}  #print the member name and the avg total for each user.
    save as awkao then run within the console with the following statement:
    Code:
     $ awk -f awkao AOTOP10POSTERS
    output is:
    AO'S Top 10 Posters
    (Today's date )
    Members name
    ================================
    Tedol 2261
    souleman 2031
    Korpdeath 1744
    Terr 1718
    ac1dsp3ctrum 1466
    s0nic 1422
    Ennis 1272
    conf1rm3d Kill 1168
    valhallen 1149
    victorkaum 1124

    10 records processed.
    tedobl 753
    souleman 677
    KorpDeath 591
    Terr 572
    ac1dsp3ctrum 488
    s0nic 477
    Ennis 424
    conf1m3d k1ll 389
    valhallen 383
    victorkaum 374

    Sweet :-V it works.
    btw, This is the end of the short Intro to awk. If U found this helpful let me know then I'll make a 2nd tut going more in depth using advance awk features. thaxs for UR time 2 read this Hey! UNIX is user-friendly, it's just picky about it's friends.

  2. #2
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,915
    For you windows users out there who are interested in playing with awk.... there is a version available for Win32 called gawk.

    It is part of the GNU Win32 Project... you will also find many other gnu/open source *nix applications available here.


    http://gnuwin32.sourceforge.net/

  3. #3
    Senior Member
    Join Date
    Aug 2002
    Posts
    651
    Pardon my ignorance, as I am fairly new to programming and languages of this nature, but what advantages does this have over Perl or a tool like grep? Thanks for the tut phaza, and thanks for the link HTRegz. I just finished downloading it for Windows.


    Cheers,

    t2k2
    Opinions are like holes - everybody\'s got\'em.

    Smile

  4. #4
    first, it has none over perl.
    second, grep matches patterns in a file
    third awk process's files; which later we can maniuplate with test to get a certain look in output file. grep is a cmd-line tool as well as awk. awk is a command that lets U do programming logic on input. if running a *nix flavor do a man awk. or try
    awk To me since I love programming languages, awk is a handy tool for certain tasks to give a file a certain look. That I want. It can't do nearly what perl or python my favorite can do. So don't throw them out the window. Hope, this helps-thanxs for the feed back.

    PS,
    Awk is merely a editor.
    Sed is a stream editor, handy for deleting headers of emails that I want to save in a folder. Awk is very powerful when combine with a shell script.

  5. #5
    Senior Member
    Join Date
    Aug 2002
    Posts
    651
    Thanks for clarifying that for me. It looks like a pretty handy tool. I just wish I had more time to play with programming and scripting languages and what not...
    Opinions are like holes - everybody\'s got\'em.

    Smile

  6. #6
    thanks for the tut phaza, amazing that someone did not have it here already. look forward to other updates on this topic.

    awk has helped me out a bunch in the past, and is certainly a viable tool for any *nix user-owner-sa, etc, and it is very flexible in usage.

    if you like spending $$$, i would also recommend the O'Reilly book on sed/awk. great reading.

    just for grins, I took a quick look to add to this and found a few more sources:

    http://www.vectorsite.net/tsawk.html

    http://stud.wsi.edu.pl/~robert/awk/

    thanks again phaza!

    \"Quis custodiet ipsos custodes?\"
    -Juvenal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •