Home >System Tutorial >LINUX >Pattern filtering method for text or string using awk
When filtering text, sometimes you may want to mark certain lines in a file or string based on a given condition or using a specific pattern that can be matched. It's very easy to accomplish this task using awk, and it's one of the few features of awk that may help you.
Let's take a look at the following example. For example, you have a shopping list with the food you want to buy. Its name is food_prices.list. The food names and corresponding prices it contains are as follows:
$ cat food_prices.list No Item_Name Quantity Price 1 Mangoes 10 $2.45 2 Apples 20 $1.50 3 Bananas 5 $0.90 4 Pineapples 10 $3.46 5 Oranges 10 $0.78 6 Tomatoes 5 $0.55 7 Onions 5 $0.45
Then, you want to use a (*) symbol to mark those foods with a unit price greater than $2, then you can do this by running the following command:
$ 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 that there is a (*) mark at the end of the line containing mango and pineapple. If you check their price per unit, you can see that they are indeed over $2 per unit.
In this example, we have used two modes:
The first pattern: / */$[2-9]/.[0-9][0-9] */ will get rows containing food with a unit price greater than $2.
The second pattern: /*/$[0-1]/.[0-9][0-9] */ will find rows where the unit price of food is less than $2.
What exactly does the above command do? This file has four fields, and when the pattern matches a line containing a food unit price greater than $2, it will output all four fields and mark the line with a (*) symbol at the end.
The second mode simply outputs the other rows containing food with unit prices less than $2, as they appear in the input file food_prices.list.
This way you can use the pattern to filter out those food items that cost more than $2. Although there are some problems with the above output, the lines with the (*) symbol are not formatted like other lines, which makes The output appears unclear.
We also saw the same problem in the second part of the awk series, but we can solve it in the following two ways:
1. You can use the printf command 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 $0 field. Awk uses the variable 0 to store the entire input line. For the above problem, this method is very convenient, 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 items
in conclusion
That's all. Using the awk command, you can use pattern matching to filter text in several simple ways, helping you mark certain lines of text or strings in a file.
The above is the detailed content of Pattern filtering method for text or string using awk. For more information, please follow other related articles on the PHP Chinese website!