Server Administration

  Home arrow Server Administration arrow Page 3 - Understanding Awk in the UNIX Shell
SERVER ADMINISTRATION

Understanding Awk in the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2009-12-02

    Table of Contents:
  • Understanding Awk in the UNIX Shell
  • The structure of Awk
  • Patterns
  • Commands and variables

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Understanding Awk in the UNIX Shell - Patterns


    (Page 3 of 4 )

    The awk program lines are built from rules. You can compose a rule with the help of a pattern and an action or statement. The last one should be enclosed inside a set of {} brackets. For instance:

    pattern {action/statement}

    One of the most basic commands that you can insert into the awk action/statement list is print. This will print the values of the variables followed by the command, and close it with a new line. For example, the following will print "feel rain" on the screen:

    echo 'I want to feel the rain" | awk '{print $4, $6}'

    The pattern can be any regular expression. Now if you missed my article related to this, I will not start all over again. Just make a fast search under my name and you should find it under the name of Regular Expressions in the UNIX Shell.

    If you add the pattern also, the statement or action will be executed only if the record matches the pattern. If you enter no action or statement, then the default will be called. This will print the entire record (aka print $0). For example, the following line will print on the screen the Immortal word, as the same input words do indeed start with the "Im" character sequence:

    echo Immortal | awk '{/^Im/}'

    Still, the entire system is just a little more complicated than that. The table below will make it all clear.

    Pattern

    What it means

    BEGIN

    This will be executed before processing any of the lines.

    Usually a good place to make initializations, determine the field separator characters and so on.

    END

    This will be executed after processing all the lines.

    Use this to print out final results, like an addition of a column and tasks that conclude the processing procedure and need to be executed only once.

    /regex/

    A simple regular expression, just as I introduced you to in the prior lines.

    pattern1 && pattern2

    The "and" operator. Both of the patterns must match in order to execute the action or statement that follows in the brackets.

    pattern1 || pattern2

    Same as above, with the difference that this is the "or" operator. If any of the patterns match, we are good to go.

    !pattern

    Negation. If the pattern does not match, step forward to the statement.

    pattern ? pattern1: pattern2

    This is a sort of if. If the pattern matches, then the final word will be if pattern1 matches, otherwise pattern2 will take over the significant role.

    Relational expression

    The evaluation of the following relational operators (related to mathematical comparison or string matching):

    < --- Smaller than

    > --- Greater than

    <= --- Less than or equal

    >= --- Greater than or equal

    == --- Equal

    != --- Not equal

    ~ --- Match (for strings)

    ~! --- No match(for strings)

     

    For example, on the pattern matching:

    $2 ~ /^I/

    => True if the second field starts with a great I letter.

     

    Note the other relational operators may work for strings also, with the mention that a step-by-step character comparison will be made. The expression is true if, after evaluation, we get a non-zero number value or a not empty string.

    More Server Administration Articles
    More By Gabor Bernat

    blog comments powered by Disqus

    SERVER ADMINISTRATION ARTICLES

    - Server Responses to Client Communication
    - Authentication in Client/Server Communication
    - Client/Server Communication
    - Understanding Awk in the UNIX Shell
    - Stream Editor in the UNIX Shell
    - Processes in the UNIX Shell
    - Migrating from Windows to Wine
    - Wine: Not Another Emulator
    - Preventive Measures to Block SSH Attacks
    - Monitoring Temperatures with Cacti
    - Cacti: RRDTool-based Graphing Solution
    - Network Magic 5.0 Review
    - Netfilter and Iptables Overview
    - Installing and Configuring Squid
    - Clickfree PC Backup Systems Compared


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