First we need to know that awk can automatically separate the input rows into several fields. Each field is a set of characters, and they are separated from the other fields by an internal field delimiter.
If you are familiar with unix/linux or if you know about Bash shell programming, then you should be aware of what is an internal field delimiter (IFS) variable. The default IFS in awk is tabs and spaces.
The field delimiter in awk works as follows: When reading a line of input, it is divided into different fields according to the specified IFS, the first set of characters is field one, can be accessed through $, the second set of characters is field two, can be accessed through the $ $, the third set of characters is field three, can be accessed through $ 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
Create a file on Linux
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.txtTecMint.comisthe} '
From the output above you can see that the first three characters of the field are separated by a space separator:
- Field one is "tecmint.com" and can be accessed by $.
- Field Two is the "is" and can be accessed via $.
- Field three is "the" and can be accessed through $ A.
If you look at the output, you can see that the output field values are not separated, which is the default behavior of the print function.
To make the output look clearer, the output field values are separated by spaces, and you need to add (,) operators.
$ Awk '//{print $, $ $, $;} ' tecmintinfo.txtTecMint.com is the
It is important to remember and very importantly, ($) the use in awk and in shell scripts is quite different!
In a shell script, ($) is used to get the value of a variable. In Awk, ($) is used only when the value of the field is obtained and cannot be used to get the value of the variable.
Example 2: Let's look at an example that uses a multiple-line file named My_shoping.list.
No item_name unit_price Quantity Price1 Mouse #20, 1 #20, 0002 Monitor #500, 1 #500, 0003 ram_chips #150, 2 #300, 0004 ethernet_cables #30, 4 #120, 000
If you only want to output the unit price of each item on your shopping list, just run the following command:
$ Awk '//{print $, $ my_shopping.txt} ' item_name unit_pricemouse #20, 000Monitor #500, 000ram_chips #150, 000ethernet_cab Les #30, 000
You can see that the output above is not clear enough, and Awk has a printf command that can 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_pricemouse #20, 000Monitor # 500,000ram_chips #150, 000ethernet_cables #30, 000
Summary
When you use awk to filter text or strings, the function of field editing is very important. It can help you get specific columns from the data in a table. It is important to remember that the use of the ($) operator in awk is different from its use in shell scripting!
Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/
How to use awk to output fields and columns in text