Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to filter text or strings with Awk mode

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Most people do not understand the knowledge points of this article "how to use Awk mode to filter text or strings", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Awk mode to filter text or strings" article.

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.listNo 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 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 modes: the first mode: / * / $[2-9] /. 0-9 * / will get rows that contain food with a unit price greater than that. The second mode [0-1] /. 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

Use Awk and Printf to filter and output projects

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

Use Awk and variables to filter and output projects

The above is about the content of this article on "how to use Awk mode to filter text or strings". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report