Server Administration

  Home arrow Server Administration arrow Page 2 - 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 - The structure of Awk


    (Page 2 of 4 )

    There are multiple versions of Awk. I will present the traits belonging to the one present in GNU Linux. If you read my article about the sed (stream editor) you can already picture how it works. And if you haven't, I recommend that you do. First read in the data (by default, use the standard input). Process it and tokenize it into rows and records. Execute a script on the rows. Print out the result on the standard output.

    The script is a little command list that must be executed on every single line of the input stream. Due to the fact that the awk scripts can turn out to be quite long, to the point where it will no longer fit into a single row of the terminal, you can also write them on a separate file. Later, you can call the specific file to take the place of the awk script.

    Accomplish this with the switch -f option. Furthermore, the script files should have the .awk extension. This is only so we can tell the difference between the files.

    cat alfa.txt | awk -f script.awk

    The first line of these scripts is just like that for the bash shell scripts; it should start with the following line:

    #!/usr/bin/awk -f

     

    With this, if we run the script file directly, the shell will figure out what to do with it. Furthermore, we can specify the field separator characters at the start. By default, this is the regular expression [ t]+, meaning one or more occurrences of tabs and spaces. You can also initialize a variable of the script with an external value with the -v option switch. The whole syntax is as follows:

    -v variable_Name=variable_start_Value;

     

     

    From the point of view of awk, lines are just a sequence of fields separated by a character or character sequence. It also likes to call lines as records. For example, let there be the input:

     

    Held up so high I am not...

    Tell me.

     

    Here we have two records. In the first one we have seven fields where the field separator is a single white space. While we process each line, we can refer to the entire line with the $0 syntax. Furthermore, you can address fields individually with the $1,$2,$3... syntax, where the number after the dollar sign refers to the n-th field.

    Therefore in the upper example in the second record, while $0 is the whole line, $1 will be equal to the string "Tell" and $2 to the "me." Of course we can change this during the process operation and also refer to them with the help of the variables:

    k=1

    print $k

    The upper example will print the first field corresponding to the $1. You can follow information about the script, with the help of some general variables. These exist for all awk scripts. With awk you can process multiple files also, one after another.

    Inside the $NR you will find the number of records you read in from all of the files. In the $FNR is the number of lines processed from the current file that is filtered with awk. The number of the fields is inside the FN (Field number). With these you can easily address the last field in each record as $FN.

    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 4 - Follow our Sitemap