Linux Network Namespace Introduction - Docker Kubernetes Lab 0.1
Linux Network Namespace Introduction - Docker Kubernetes Lab 0.1
Dave McKay
It’s a full scripting language, as well as a complete text manipulation toolkit for the
command line. If this article whets your appetite, you can check out every detail
about awk and its functionality.
Let’s take a look at the simplest type of awk program. It has no pattern, so it matches every
line of text fed into it. This means the action is executed on every line. We’ll use it on the
output from the who command.
awk prints the first field and discards the rest of the line.
We can print as many fields as we like. If we add a comma as a separator, awk prints a space
between each field.
Join 425,000 subscribers and get a daily digest of features, articles, news, and trivia.
By submitting your email, you agree to the Terms of Use and Privacy Policy.
We type the following to also print the time the person logged in (field four):
who | awk '{print $1,$4}'
There are a couple of special field identifiers. These represent the entire line of text and the
last field in the line of text:
We’ll type the following to bring up a small text file that contains a short quote attributed to
Dennis Ritchie:
cat dennis_ritchie.txt
We want awk to print the first, second, and last field of the quote. Note that although it’s
wrapped around in the terminal window, it’s just a single line of text.
We don’t know that “simplicity.” is the 18th field in the line of text, and we don’t care. What
we do know is it’s the last field, and we can use $NF to get its value. The period is just
considered another character in the body of the field.
We’ll use the OFS (output field separator) variable to put a separator between the month, day,
and year. Note that below we enclose the command in single quotes ('), not curly braces
({}):
date | awk 'OFS="/" {print$2,$3,$6}'
For our example of a BEGIN rule, we’ll print the entire quote from the dennis_ritchie.txt file
we used previously with a title above it.
Note the BEGIN rule has its own set of actions enclosed within its own set of curly braces ({}).
We can use this same technique with the command we used previously to pipe output from
who into awk. To do so, we type the following:
The output contains the name of the user account (or application or daemon name) and the
home folder (or the location of the application).
Adding Patterns
If all we’re interested in are regular user accounts, we can include a pattern with our print
action to filter out all other entries. Because User ID numbers are equal to, or greater than,
1,000, we can base our filter on that information.
We type the following to execute our print action only when the third field ($3) contains a
value of 1,000 or greater:
awk -F: '$3 >= 1000 {print $1,$6}' /etc/passwd
The pattern should immediately precede the action with which it’s associated.
We can use the BEGIN rule to provide a title for our little report. We type the following, using
the (\n) notation to insert a newline character into the title string:
awk -F: 'BEGIN {print "User Accounts\n-------------"} $3 >= 1000 {print $1,$6}' /etc/passwd
Patterns are full-fledged regular expressions, and they’re one of the glories of awk.
Let’s say we want to see the universally unique identifiers (UUIDs) of the mounted file
systems. If we search through the /etc/fstab file for occurrences of the string “UUID,” it
ought to return that information for us.
It finds all occurrences of “UUID” and prints those lines. We actually would’ve gotten the
same result without the print action because the default action prints the entire line of text.
For clarity, though, it’s often useful to be explicit. When you look through a script or your
history file, you’ll be glad you left clues for yourself.
The first line found was a comment line, and although the “UUID” string is in the middle of
it, awk still found it. We can tweak the regular expression and tell awk to process only lines
that start with “UUID.” To do so, we type the following which includes the start of line token
(^):
awk '/^UUID/ {print $0}' /etc/fstab
That’s better! Now, we only see genuine mount instructions. To refine the output even
further, we type the following and restrict the display to the first field:
awk '/^UUID/ {print $1}' /etc/fstab
If we had multiple file systems mounted on this machine, we’d get a neat table of their
UUIDs.
Built-In Functions
awk has many functions you can call and use in your own programs, both from the command
line and in scripts. If you do some digging, you’ll find it very fruitful.
To demonstrate the general technique to call a function, we’ll look at some numeric ones.
For example, the following prints the square root of 625:
awk 'BEGIN { print sqrt(625)}'
This command prints the arctangent of 0 (zero) and -1 (which happens to be the
mathematical constant, pi):
awk 'BEGIN {print atan2(0, -1)}'
In the following command, we modify the result of the atan2() function before we print it:
awk 'BEGIN {print atan2(0, -1)*100}'
Functions can accept expressions as parameters. For example, here’s a convoluted way to
ask for the square root of 25:
awk 'BEGIN { print sqrt((2+3)*5)}'
awk Scripts
If your command line gets complicated, or you develop a routine you know you’ll want to use
again, you can transfer your awk command into a script.
The BEGIN rule carries out the preparatory steps, while the END rule displays the counter value.
The middle rule (which has no name, nor pattern so it matches every line) modifies the
second field, prints the line, and increments the counter.
The first line of the script tells the shell which executable to use (awk, in our example) to run
the script. It also passes the -f (filename) option to awk, which informs it the text it’s going to
process will come from a file. We’ll pass the filename to the script when we run it.
We’ve included the script below as text so you can cut and paste:
#!/usr/bin/awk -f
BEGIN {
# set the input and output field separators
FS=":"
OFS=":"
# zero the accounts counter
accounts=0
}
{
# set field 2 to nothing
$2=""
# print the entire line
print $0
# count another account
accounts++
}
END {
# print the results
print accounts " accounts.\n"
}
Save this in a file called omit.awk. To make the script executable, we type the following using
chmod:
chmod +x omit.awk
Now, we’ll run it and pass the /etc/passwd file to the script. This is the file awk will process for
us, using the rules within the script:
./omit.awk /etc/passwd
The “x” entries in the second field were removed, but note the field separators are still
present. The lines are counted and the total is given at the bottom of the output.
That power is harnessed by the simple concept of rules that contain patterns, that select the
text to process, and actions that define the processing.
READ NEXT