In this section of the Awk series, we'll see one of the most important features of awk, field editing.
First, we need to know that AWK can automatically separate the lines entered into several fields. Each field is a set of characters that are separated from the other fields by an internal field delimiter.
If you are familiar with unix/linux or bash shell programming, then you should know what an internal field delimiter (IFS) variable is. The default IFS in awk is tabs and spaces.
The field separators in awk work as follows: When one line of input is read, it is divided into different fields according to the specified IFS, and the first set of characters is a field, which can be accessed by $, and the second set of characters is field two, which can be accessed by $ $, and the third set of characters is field three, which is accessed by And so on, until the last set of characters.
To better understand awk's field edits, let's look at one of the following examples:
Example 1: I created a text file named Tecmintinfo.txt.
# VI Tecmintinfo.txt
# Cat Tecmintinfo.txt
Then on the command line, I try to use the following command to output the first, second, and third fields from the text tecmintinfo.txt.
$ Awk '//{print $ $} ' Tecmintinfo.txt
Tecmint.comisthe
As you can see from the above output, the first three fields are separated by a space-delimited character:
Field one is "tecmint.com" and can be accessed by $.
Field two is ' is ' and can be accessed by $ $.
Field three is "the" and can be accessed through $ $.
If you look at the output, you can see that the field value of the output is not separated, which is the default behavior of the print function.
To make the output look clearer, the field values of the output are separated by spaces, and you need to add (,) the operator.
$ Awk '//{print $ $ $ $} ' Tecmintinfo.txt
Tecmint.com is the
It's important to remember that ($) the use of awk in the shell script is very different!
In the shell script, ($) is used to get the value of the variable. In Awk, ($) is used only when getting the value of a field and cannot be used to get the value of a variable.
Example 2: Let's take another example and use a file that contains multiple lines named My_shoping.list.
No item_name unit_price Quantity Price
1 Mouse #20 1 #20, 000
2 Monitor #500 1 #500, 000
3 ram_chips #150 2 #300, 000
4 Ethernet_cables #30 4 #120, 000
If you only want to output the unit price for each item on your shopping list, you only need to run the following command:
$ Awk '//{print $, $ $} ' My_shopping.txt
Item_name Unit_price
Mouse #20, 000
Monitor #500, 000
Ram_chips #150, 000
Ethernet_cables #30, 000
You can see that the output is not clear enough, and Awk has a printf command to help you format the output.
Use printf to format the output of Item_name and Unit_price:
$ awk '//{printf '%-10s%s\n ', $ $ $} ' My_shopping.txt
Item_name Unit_price
Mouse #20, 000
Monitor #500, 000
Ram_chips #150, 000
Ethernet_cables #30, 000
Summarize
The function of field editing is very important when using awk to filter text or strings. It can help you get a specific column from the data in a table. It is important to remember that the usage of the ($) operator in awk is different from that used in the shell script!