In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to use awk to output the fields and columns in the text. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Example 1: I created a text file called tecmintinfo.txt.
# vi tecmintinfo.txt
# cat tecmintinfo.txt
Create a file on Linux
Then on the command line, I try to output the first, second, and third fields from the text tecmintinfo.txt using the following command.
$awk'/ / {print $1 $2 $3} 'tecmintinfo.txt
TecMint.comisthe
As you can see from the output above, the characters of the first three fields are output with spaces as delimiters:
Field one is "TecMint.com", which can be accessed through $1. Field 2 is "is", which can be accessed through $2. Field 3 is "the" and can be accessed through $3.
If you look at the output, you can see that the output field values are not separated, which is the default behavior of the print function.
In order to see the output more clearly, the field values of the output are separated by spaces, and you need to add (,) operators.
$awk'/ {print $1, $2, $3;} 'tecmintinfo.txt
TecMint.com is the
It is important to keep in mind that the use of ($) in awk is very different from that in shell scripts!
In the shell script, ($) is used to get the value of the variable. In awk, ($) is used only when getting the value of a field and cannot be used to get the value of a variable.
Example 2: let's look at another example, using a file called my_shoping.list that contains multiple lines.
No Item_Name Unit_Price Quantity Price
1 Mouse # 20000 1 # 20000
2 Monitor # 500000 1 # 500000
3 RAM_Chips # 150000 2 # 300000
4 Ethernet_Cables # 30000 4 # 120000
If you only want to output the unit price of each item on the shopping list, you just need to run the following command:
$awk'/ / {print $2, $3} 'my_shopping.txt
Item_Name Unit_Price
Mouse # 20000
Monitor # 500000
RAM_Chips # 150000
Ethernet_Cables # 30000
You can see that the output above is not clear enough. Awk also has a command for printf to help you format the output.
Use printf to format the output of Item_Name and Unit_Price:
$awk'/ / {printf "%-10s% Spura", $2, $3} 'my_shopping.txt
Item_Name Unit_Price
Mouse # 20000
Monitor # 500000
RAM_Chips # 150000
Ethernet_Cables # 30000
The above is the editor for you to share how to use awk to output the fields and columns in the text, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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.