How to Use awk to output fields and columns in text

Source: Internet
Author: User

How to Use awk to output fields and columns in text

First, we need to know that awk can automatically separate the input rows into several fields. Each field is a group of characters separated from other fields by an internal field separator.

If you are familiar with Unix/Linux or bash shell programming, you should know what is the internal field separator (IFS) variable. In awk, the default IFS is tab and space.

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 character is Field 1, which can be accessed through $1, the second group of characters is Field 2, which can be accessed through $2. The third group of characters is Field 3, which can be accessed through $3, and so on until the last group of characters.

To better understand the field editing of awk, let's take a look at the following example:

Example 1: I created a text file named tecmintinfo.txt.

# vi tecmintinfo.txt# cat tecmintinfo.txt

Create a file in Linux

Then, in the command line, I tried to use the following command to output the first, second, and third fields from the text tecmintinfo.txt.

$ awk '//{print $1 $2 $3 }' tecmintinfo.txtTecMint.comisthe

From the above output, you can see that the characters in the first three fields are output using spaces as separators:

  • Field 1 is "TecMint.com" and can be accessed through $1.
  • The second field is "is", which can be accessed through $2.
  • Field 3 is "the" and can be accessed through $3.

If you observe the output, you can find that the output field values are not separated, which is the default act of the print function.

To make the output clearer, use spaces to separate the output field values. You need to add the (,) operator.

$ awk '//{print $1, $2, $3; }' tecmintinfo.txtTecMint.com is the

Remember that ($) is completely different in awk and shell scripts!

In shell scripts, ($) is used to obtain the value of a variable. In awk, ($) is used only when obtaining the value of a field and cannot be used to obtain the value of a variable.

Example 2: Let's look at another example and use a file named my_shoping.list that contains multiple rows.

No Item_Name Unit_Price Quantity Price1 Mouse #20,000 1 #20,0002 Monitor #500,000 1 #500,0003 RAM_Chips #150,000 2 #300,0004 Ethernet_Cables #30,000 4 #120,000 

If you only want to output the unit price of each item in the shopping list, you only need to run the following command:

$ awk '//{print $2, $3 }' my_shopping.txt Item_Name Unit_PriceMouse #20,000Monitor #500,000RAM_Chips #150,000Ethernet_Cables #30,000

The output above is not clear enough. awk also has a printf command to help you format the output.

Use printf to format output of Item_Name and Unit_Price:

$ awk '//{printf "%-10s %s/n",$2, $3 }' 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 field editing function is very important. It helps you get specific columns from a table's data. Remember that the usage of the ($) Operator in awk is different from that in shell scripts!

From: https://linux.cn/article-7587-1.html

Address: http://www.linuxprobe.com/awk-print-field-cols.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.