Saturday 6 August 2011

How to Run AWK Programs?

In my previous post I Explained about the AWK, In this post we have to check How to run AWK programs.
The basic function of awk is to search files for lines (or other units of text) that contain certain patterns. When a line matches one of the patterns, awk performs specified actions on that line. awk keeps processing input lines in this way until it reaches the end of the input files. An awk program looks like this:

pattern { action }
     pattern { action }
     ...
There are several ways to run an awk program. If the program is short, it is easiest to include it in the command that runs awk, like this:
awk 'program' input-file1 input-file2 ...
When the program is long, it is usually more convenient to put it in a file and run it with a command like this:
awk -f program-file input-file1 input-file2 ...
Once you are familiar with awk, you will often type in simple programs the moment you want to use them. Then you can write the program as the first argument of the awk command, like this:
     awk 'program' input-file1 input-file2 ...
where program consists of a series of patterns and actions, as described earlier.
This command format instructs the shell, or command interpreter, to start awk and use the program to process records in the input file(s).

There are single quotes around program so the shell won't interpret any awk characters as special shell characters. The quotes also cause the shell to treat all of program as a single argument for awk, and allow program to be more than one line long.

This format is also useful for running short or medium-sized awk programs from shell scripts, because it avoids the need for a separate file for the awk program.

Running awk Without Input Files
You can also run awk without any input files. If you type the following command line:
awk 'program'
awk applies the program to the standard input, which usually means whatever you type on the terminal. This continues until you indicate end-of-file by typing ctrl-d.
Example:
awk "BEGIN { print \"Don't Panic!\" }" 

This program does not read any input. The `\' before each of the inner double quotes is necessary because of the shell's quoting rules—in particular because it mixes both single quotes and double quotes


Running Long Programs
Sometimes your awk programs can be very long. In this case, it is more convenient to put the program into a separate file. In order to tell awk to use that file for its program, you type:


awk -f source-file input-file1 input-file2 ...
The -f instructs the awk utility to get the awk program from the file source-file. Any file name can be used for source-file. For example, you could put the program:
BEGIN { print "Don't Panic!" }
into the file advice. Then this command:
awk -f advice
does the same thing as this one:
awk "BEGIN { print \"Don't Panic!\" }"

Executable awk Programs


Once you have learned awk, you may want to write self-contained awk scripts, using the `#!' script mechanism. For example, you could update the file advice to look like this:
#! /bin/awk -f
     BEGIN { print "Don't Panic!" }

After making this file executable (with the chmod utility), simply type `advice' at the shell and the system arranges to run awk as if you had typed `awk -f advice':
chmod +x advice
     $ advice
     -| Don't Panic!

Comments in awk programs
In the awk language, a comment starts with the sharp sign character (`#') and continues to the end of the line. The `#' does not have to be the first character on the line. The awk language ignores the rest of a line following a sharp sign. For example, we could have put the following into advice:
# This program prints a nice friendly message.  It helps
     # keep novice users from being afraid of the computer.
     BEGIN    { print "Don't Panic!" }

Note: You
can enclose small to medium programs in single quotes, in order to
keep your shell scripts self-contained. When doing so, don't
put an apostrophe (i.e., a single quote) into a comment (or anywhere
else in your program)
For example, look at the following:


awk '{ print "hello" } # let's be cute'
     > '
     error--> awk: can't open file be
     error-->  source line number 1

Some simple Examples:
awk '/foo/ { print $0 }' BBS-list
This command runs a simple awk program that searches the input file BBS-list for the character string `foo'.When lines containing `foo' are found, they are printed because `print $0' means print the current line. Here is what this program prints:
awk '/foo/ { print $0 }' BBS-list
     -| fooey        555-1234     2400/1200/300     B
     -| foot         555-6699     1200/300          B
     -| macfoo       555-6480     1200/300          A
     -| sabafoo      555-2127     1200/300          C



Many practical awk programs are just a line or two. Following is a collection of useful, short programs to get you started.
Print the length of the longest input line:
awk '{ if (length($0) > max) max = length($0) }
END { print max }' data
Print every line that is longer than 80 characters: 
          awk 'length($0) > 80' data 


Print the length of the longest line in data:
expand data | awk '{ if (x < length()) x = length() }
                        END { print "maximum line length is " x }' 
Print every line that has at least one field: 
awk 'NF > 0' data
This is an easy way to delete blank lines from a file (or rather, to create a new file similar to the old file but from which the blank lines have been removed).


Print seven random numbers from 0 to 100, inclusive: 
awk 'BEGIN { for (i = 1; i <= 7; i++) print int(101 * rand()) }'
Print  the total number of bytes used by files
ls -l files | awk '{ x += $5 } END { print "total bytes: " x }'
Print  the total number of kilobytes used by files
ls -l files | awk '{ x += $5 } END { print "total K-bytes: " (x + 1023)/1024 }'     
Print a sorted list of the login names of all users: 
awk -F: '{ print $1 }' /etc/passwd | sort
Count  the lines in a file: 
awk 'END { print NR }' data
Print the even-numbered lines in the data file:
awk 'NR % 2 == 0' data 
If you use the expression `NR % 2 == 1' instead, the program would print the odd-numbered lines.


If you want to view more examples, visit AWK Programming

No comments:

Post a Comment