0% found this document useful (0 votes)
21 views2 pages

Linux Bash Beginners - Guide 37

The document discusses awk commands and how to use awk to print selected fields from input files. It shows how to print specific columns from the output of commands like ls -l by referring to the field variables $1, $2 etc. It also demonstrates formatting the output by inserting tabs and text between the selected fields.

Uploaded by

marco rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Linux Bash Beginners - Guide 37

The document discusses awk commands and how to use awk to print selected fields from input files. It shows how to print specific columns from the output of commands like ls -l by referring to the field variables $1, $2 etc. It also demonstrates formatting the output by inserting tabs and text between the selected fields.

Uploaded by

marco rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Gawk commands

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)

Printing selected fields


The print command in awk outputs selected data from the input file.
When awk reads a line of a file, it divides the line in fields based on the specified input field
separator, FS, which is an awk variable (see Section 6.3.2). This variable is predefined to be one or
more spaces or tabs.
The variables $1, $2, $3, ..., $N hold the values of the first, second, third until the last field of an
input line. The variable $0 (zero) holds the value of the entire line. This is depicted in the image
below, where we see six colums in the output of the df command:

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.

You might also like