How to Use awk to filter text or string by Mode

Source: Internet
Author: User

How to Use awk to filter text or string by Mode
GuideWhen filtering text, you may want to mark certain lines of a file or a string of several rows based on a given condition or a specific pattern that can be matched. It is very easy to use awk to complete this task, which is one of the several features that may be helpful to you in awk.


Let's take a look at the following example. For example, you have a shopping list for the food you want to buy. Its name is food_prices.list. The name and price of the food contained in it are as follows:

$ cat food_prices.list No  Item_Name       Quantity    Price1   Mangoes            10       $2.452   Apples             20       $1.503   Bananas            5        $0.904   Pineapples         10       $3.465   Oranges            10       $0.786   Tomatoes           5        $0.557   Onions             5        $0.45

Then, if you want to use a (*) symbol to mark foods with a unit price greater than $2, you can run the following command to achieve this purpose:

$ awk '/ */$[2-9]/.[0-9][0-9] */ { print $1, $2, $3, $4, "*" ; } / */$[0-1]/.[0-9][0-9] */ { print ; }' food_prices.list

Print items with a unit price greater than $2

From the above output, you can see thatMangoAndPineappleThere is a (*) mark at the end of that line. If you check their unit prices, you can see that their unit prices indeed exceed $2.

In this example, we have used two modes:
The first mode:/*/$ [2-9]/. [0-9] [0-9] */will get the rows containing food with a unit price greater than $2.
The second mode:/*/$ [0-1]/. [0-9] [0-9] */searches for the rows whose food unit price is less than $2.

What did the above command do? This file has four fields. When the pattern matches a row containing a food unit price greater than $2, It outputs all four fields and adds a (*) at the end of the row (*) symbol.

The second mode simply outputs other rows containing less than $2 food at a unit price and displays them in the input file food_prices.list.

In this way, you can use the pattern to filter out food items whose prices exceed $2, although the above output has some problems (*) the lines of the symbol are not formatted as other lines, which makes the output not clear enough.

We also saw the same problem in the second part of the awk series, but we can solve it using the following two methods:

1. the printf command can be used as follows, but it is long and boring:

$ awk '/ */$[2-9]/.[0-9][0-9] */ { printf "%-10s %-10s %-10s %-10s/n", $1, $2, $3, $4 "*" ; } / */$[0-1]/.[0-9][0-9] */ { printf "%-10s %-10s %-10s %-10s/n", $1, $2, $3, $4; }' food_prices.list 

Use Awk and Printf to filter and output Projects

2. Use the $0 field. Awk Variables0 to store the entire input line. This method is very convenient for the above problems, and it is also simple and fast:

$ awk '/ */$[2-9]/.[0-9][0-9] */ { print $0 "*" ; } / */$[0-1]/.[0-9][0-9] */ { print ; }' food_prices.list 

Use Awk and variables to filter and output Projects

Conclusion

This is all the content. With the awk command, you can use several simple methods to filter text by using pattern matching to help you mark certain lines of text or string in a file.

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

Address: http://www.linuxprobe.com/awk-filt-string.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.