Server Administration

  Home arrow Server Administration arrow Page 4 - 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 - Commands and variables


    (Page 4 of 4 )

    The commands go between the {} brackets. Now you can enumerate them in a single line using the ";" separator character or a single command in a single line, as the new line character is also a command separator character. A command structure (awk script) usually looks as follows:

    BEGIN {intro commands}

    Pattern {Commands to be executed at every record if the pattern matches}

    END {closing commands}

    If you do not need to use any of these you can just skip them and not include them in the script. The character "#" is for comments. Any text found after this character will not be considered in the interpretation of the script. The awk variables are very similar to those present in the C language from the point of view of handling them.

    However, here we do not need to declare them. They are created in the moment of declaration and the type is dependent on the environment. If you assign a number to it, they will be numbers (always represented as a float number); if you assign a string to it, then they will be of a string type.

    Here is a list of internal variables that are created automatically at the start of the script (and you do not need to declare/modify them unless you want to change them); this will make your programming easier:

    Variable

    What it contains?

    Default value

    ARGC

    Contains the number of arguments on the command line (Command Line Argument Count )

    None

    FILENAME

    The input file's name (FILENAME)

    None

    FS

    The field separator characters. If this is empty, every character will be in a separate column. (Field Separator)

    Space and tab => [t]+

    RS

    The record separator characters. (Record Separator)

    Newline character=> n

    IGNORECASE

    If not zero at the regular expression pattern matching, an ignore case compare will be made.

    0

    NF

    How many fields are in the input record?

    (Number of Fields in the current input record)

    none

    NR

    How many lines have already been processed?

    (Numbers of records seen so far)

    None

    OFMT

    How should it format the numbers that will be printed?

    (The output format for numbers)

    "%.6g"

    OFS

    The output field separator character.

    Space

    ORS

    Output Record Separator.

    With this we separate records that are printed out. If you change this to an empty string, the output will be not separated.

    New line.

    When you declare some string constants, you can use the escape characters of the C language:

    Escape character

    What it means?

    Backslash

    b

    Backspace

    f

    Form-feed

    n

    Newline

    r

    Carriage return

    t

    Horizontal Tab

    v

    Vertical Tab

    0xn

    Where n is a number.

    Hex numbers.

    c

    Literally the c character.

    For example ? => ?

    0n

    Where n is a number.

    Octal Number.

    With this I will stop. I have given you quite a chunk of information. I will leave you a little time to read it in detail and comprehend every piece of it. Nevertheless, we are still far from the end. So make sure you come back for the next article, where I will treat the other, equally important parts of awk -- like the functions, external commands, arrays, and so forth. We will also put all this together and solve a couple of problems to demonstrate the power of the language.

    Before I say farewell I would like to encourage you to rate the article. If you have any kinds of questions do not hesitate to ask them. Remember the blog commenting at the end of this article was constructed for this sole purpose. I also welcome any comments that are in the spirit of constructive criticism. Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    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