Miscellaneous

  Home arrow Miscellaneous arrow Crucial Traits of Awk
MISCELLANEOUS

Crucial Traits of Awk
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-12-09

    Table of Contents:
  • Crucial Traits of Awk
  • Functions
  • External Commands and Custom Functions
  • Arrays in awk

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Crucial Traits of Awk


    (Page 1 of 4 )

    If you're tired of the way that new programming languages often force you to learn everything all over again, you'll like awk -- especially if you know C. Awk borrows heavily from C, as you've no doubt realized if you've read the previous article in this series. This article wraps things up by presenting awk under GNU Linux.

    There seems to be a burning desire in the programming world to build languages that will make things better and simpler. Back in the old days there were just a couple of dominant languages that everyone used. However, today programming languages spread faster than a disease. To be able to understand and work in most of them has become an impossible mission.

    Under these circumstances, when someone creates some new and powerful tool/programming language that makes heavy use of the syntax and commands of an already-known programming language, it is greatly appreciated. Awk is exactly that. Its three creators, instead of trying to reinvent the wheel, just took everything they could from the C programming language and used it.

    This way, if you have a solid knowledge of this aspect of awk, understanding it fully could take only a couple of hours. If you already have some knowledge of C, this article will be much easier to comprehend. 

    There is a first part to this series: Understanding Awk in the UNIX Shell. If you missed it, I recommend that you pause in reading this one and start with that. You will need the information in that part to help you understand this one. So let's get started, shall we?

    Operators and Control Mechanism

    We left off the last article with variables. What use are variables if you cannot use them to make specific operations? For an easier way to do this, we need operators. In awk, every operator present in C is valid, and we also benefit from some new ones::

    Operator

    What it does

    ()

    Brackets for grouping

    $

    Field reference ($1)

    ++ --

    Prefix and Postfix incrementation

    ^

    Raise to power (You can also use **)

    + - !

    With one operand (sign specification), and negation

    * / %

    Multiply, Divide, Modulo

    + -

    Addition, Subtraction

    < >

    Relational evaluation

    <= >=

    != ==

    ~ !~

    Pattern match and not match

    &&

    Logical And

    ||

    Logical Or

    ?:

    A sort of "if" with the syntax: If ? then : else

    = += -=

    Assignment operators

    *= /=

    %= ^=

    There are a couple of differences as well. If you want to interlace a couple of strings for this, the operator is the empty space. Therefore the command:

    print "Avril" "Duff"

    Will first link together the two strings and give the "AvrilDuff" string as an argument to the print command. Furthermore, there is no comma operator, so you cannot make assignments like this:

    Mert="Alfa", Bert="Beta"

    I struck it out to show that it is not correct. Then they are a couple of control mechanisms that allow you to write complicated scripts, like the decision making (if ) structure, the loop structure (for, while, do while) and a few others. All of this is very much in the style of C. The if:

    if (condition) commands [ else commands]

    The square brackets indicate that the "else" branch is not mandatory. The loop syntaxes follow:

    while(condition) commands

     

    do commands while(condition)

     

    for(initialize values here; do while true this condition; execute after every loop end) commands

    Then they are the break and the continue keywords. The break will stop executing the current loop, while the "continue" will stop executing any further commands in the loop and sort of jumps right to the end of the loop. Therefore, in the case of "for" execute after every loop end will be executed, and followed by (not for all loops) the condition evaluation; if it's true, it starts all over.

    Add a little extension, and you can give this the ability to break or jump with a number followed by these keywords. This is a handy addition if you work with nested loops. Nowadays, a mechanism control is also considered the next keyword. This means that awk will stop the execution of the current record, take a new record and start all over. Look at it as a continue-like function for the records instead of the loops.

    More Miscellaneous Articles
    More By Gabor Bernat

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap