In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use awk to filter text or strings by pattern. It is very detailed and has a certain reference value. Friends who are interested must read it!
When filtering text, sometimes you may want to mark lines in a file or line string based on a given condition or with a specific pattern that can be matched. Using awk to accomplish this task is very easy, and this is one of several features in awk that may help you.
Let's take a look at the following example. For example, you have a shopping list of the food you want to buy, which is called food_prices.list. The name of the food and the corresponding price 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, if you want to use a (*) symbol to mark foods with a unit price greater than $2, you can do so 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 out items with a unit price greater than $2
From the output above, you can see that there is already a (*) mark at the end of the line containing mango and pineapple. If you check their unit price, you can see that their unit price is indeed over $2.
In this example, we have used two patterns:
The first model: / * / $[2-9] /. [0-9] [0-9] * / will get rows that contain food with a unit price greater than $2.
The second mode: / * / $[0-1] /. [0-9] [0-9] * / will find those lines 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 to a line with a unit price of food greater than $2, it outputs all four fields and adds a (*) symbol at the end of the line as a tag.
The second mode simply outputs other lines that contain food with a unit price of less than $2, as they appear in the input file food_prices.list.
This allows you to use patterns to filter out food items that cost more than $2, although there are some problems with the above output, and the lines with the (*) symbol are not formatted like other lines, which makes the output less clear.
We saw the same problem in part 2 of the awk series, but we can solve it in 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/n", $1, $2, $3, $4 "*";} / * / $[0-1] /. [0-9] [0-9] * / {printf "%-10s%-10s%-10s%-10s/n", $1, $2, $3, $4;} 'food_prices.list
2. Use the $0 field. Awk uses the variable 0 to store the entire input line. For the above questions, this approach 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
The above is all the content of the article "how to use awk to filter text or strings by pattern". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.