Linux Bash Beginners - Guide 37
Linux Bash Beginners - Guide 37
When you run awk, you specify an awk program that tells awk what to do. The program consists
of a series of rules. (It may also contain function definitions, loops, conditions and other
programming constructs, advanced features that we will ignore for now.) Each rule specifies one
pattern to search for and one action to perform upon finding the pattern.
There are several ways to run awk. If the program is short, it is easiest to run it on the command
line:
awk PROGRAM inputfile(s)
If multiple changes have to be made, possibly regularly and on multiple files, it is easier to put the
awk commands in a script. This is read like this:
awk -f PROGRAM-FILE inputfile(s)
In the output of ls -l, there are 9 columns. The print statement uses these fields as follows:
kelly@octarine ~/test> ls -l | awk '{ print $5 $9 }'
160orig
121script.sed
120temp_file
126test
120twolines
441txt2html.sh
kelly@octarine ~/test>
This command printed the fifth column of a long file listing, which contains the file size, and the
last column, the name of the file. This output is not very readable unless you use the official way of
referring to columns, which is to separate the ones that you want to print with a comma. In that
case, the default output separater character, usually a space, will be put in between each output
field.
Note that the configuration of the output of the ls -l command might be different on your system.
Display of time and date is dependent on your locale setting.
Formatting fields
Without formatting, using only the output separator, the output looks rather poor. Inserting a couple
of tabs and a string to indicate what output this is will make it look a lot better:
kelly@octarine ~/test> ls -ldh * | grep -v total | \
awk '{ print "Size is " $5 " bytes for " $9 }'
Size is 160 bytes for orig
Size is 121 bytes for script.sed
Size is 120 bytes for temp_file
Size is 126 bytes for test
Size is 120 bytes for twolines
Size is 441 bytes for txt2html.sh
kelly@octarine ~/test>
Note the use of the backslash, which makes long input continue on the next line without the shell
interpreting this as a separate command. While your command line input can be of virtually
unlimited length, your monitor is not, and printed paper certainly isn't. Using the backslash also
allows for copying and pasting of the above lines into a terminal window.