Situatie
The awk
command was named using the initials of the three people who wrote the original version in 1977: Alfred Aho, Peter Weinberger, and Brian Kernighan. These three men were from the legendary AT&T Bell Laboratories Unix pantheon. With the contributions of many others since then, awk
has continued to evolve. 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.
Solutie
What is awk Used For? Rules, Patterns, and Actions
awk
is used to filter and manipulate output from other programs and functions. awk
works on programs that contain rules comprised of patterns and actions. The action awk takes is executed on the text that matches the pattern. Patterns are enclosed in curly braces ({}
). Together, a pattern and an action form a rule. The entire awk
program is enclosed in single quotes ('
).
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.
Here’s the standard output from who
:
who
Perhaps we don’t need all of that information, but, rather, just want to see the names on the accounts. We can pipe the output from who
into awk
, and then tell awk
to print only the first field.
By default, awk
considers a field to be a string of characters surrounded by whitespace, the start of a line, or the end of a line. Fields are identified by a dollar sign ($
) and a number. So, $1
represents the first field, which we’ll use with the print
action to print the first field.
We type the following:
who | awk '{print $1}'
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.
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:
- $0: Represents the entire line of text.
- $1: Represents the first field.
- $2: Represents the second field.
- $7: Represents the seventh field.
- $45: Represents the 45th field.
- $NF: Stands for “number of fields,” and represents the last field.
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 type the following command:
awk '{print $1,$2,$NF}' dennis_ritchie.txt
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.
Adding Output Field Separators to awk Output
You can also tell awk
to print a particular character between fields instead of the default space character. The default output from the date
command is slightly peculiar because the time is plonked right in the middle of it. However, we can type the following and use awk
to extract the fields we want:
date
date | awk '{print $2,$3,$6}'
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}'
date | awk 'OFS="-" {print$2,$3,$6}'
Leave A Comment?