In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Awk: a useful data processing tool
Awk is also a great data processing tool! Sed is often used to process an entire row, while awk tends to split a row into "fields" (or fields, that is, a column). Therefore, awk is quite suitable for dealing with small data processing. The mode in which awk usually runs is as follows:
[root@www ~] # awk 'condition Type 1 {Action 1} condition Type 2 {Action 2}.' Filename
Awk is followed by two single quotes followed by curly braces {} to configure the desired processing action on the data. Awk can handle subsequent files or read standard output from the previous command. But as mentioned earlier, awk mainly deals with "data in the fields of each row", while the default "field separator is" blank key "or" [tab] key "! For example, we can use last to extract the data of the lander, and the results are as follows:
[root@www] # last-n 5 NR > = 2 {total=$2+$3+$4 > printf "s d d d .2f\ n" Total} 'Name 1st 2nd 3th Total VBird 23000 24000 25000 72000.00 DMTsai 21000 20000 23000 64000.00 Bird2 43000 42000 41000 126000.00 the above example has several important things to explain first: awk command interval: all awk actions That is, for actions within {}, if multiple commands are needed, you can use the semicolon. " "interval, or directly use the [Enter] button to separate each command. For example, in the above example, Bird pressed [enter] three times! In logical operations, if it is "equal to", be sure to use two equal signs "="! When formatting the output, in the format configuration of printf, you must add\ n before you can branch! Unlike bash shell variables, in awk, variables can be used directly without the $symbol. Using awk, you can help us deal with a lot of daily work. It is really easy to use ~ in addition, the output format of awk is often assisted by printf, so it is better for you to be a little familiar with printf! In addition, awk's action {} also supports if (condition)! For example The above command can be revised to look like this: [root@wuke shell] # cat pay.txt |\ > awk'{if (NR==1) printf "s s s s s\ n", $1, 2, 3, 4, "Total"} > NR > = 2 {total=$2+$3+$4 > printf "s d d d. 2F\ n", $1, 2, 3, 4 Total} 'Name 1st 2nd 3th Total VBird 23000 24000 25000 72000.00 DMTsai 21000 20000 23000 64000.00 Bird2 43000 42000 41000 126000.00
You can carefully compare the differences between the above two inputs ~ learn about the two grammars! Personally, I am more inclined to use the first grammar, because it will be more unified! ^ _ ^
Having learned the basics of awk, let's do some exercises to deepen our impression.
Suppose we have a file to be processed, "grade.txt":
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
# print the entire file
[root@wuke shell] # awk'{print $0} 'grade.txt
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
# print the first and fourth fields
[root@wuke shell] # awk'{print $1 "4} 'grade.txt
M.Tansley Green
J.Lulu green
P.Bunny Yellow
J.Troll Brown-3
L.Tansley Brown-2
# print header
[root@wuke shell] # awk 'BEGIN {print "Name Belt\ n -"}
{print $1 recording 4} 'grade.txt
Name Belt
-
M.Tansley Green
J.Lulu green
P.Bunny Yellow
J.Troll Brown-3
L.Tansley Brown-2
Regular expression related:
To make a field number match a regular expression, use the symbol'~ 'followed by the regular expression, or you can use an I / f statement. The conditions that follow if in awk are enclosed in ().
# the following code prints a line of $4 containing Brown
[root@wuke shell] # awk'$4~/Brown/ {print $0} 'grade.txt
J.Troll 07/99 4842 Brown-3 12 26 26
L.Tansley 05/99 4712 Brown-2 12 30 28
# inexact matching
[root@wuke shell] # awk'$3 grade.txt 48 / {print $0} 'grade.txt
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
# exact match
[root@wuke shell] # awk'$3 grade.txt = "48" {print $0} 'grade.txt
P.Bunny 02/99 48 Yellow 12 35 28
# mismatch using'! ~'
[root@wuke shell] # awk'$0 scratch grade.txt Brown Universe
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
[root@wuke shell] # awk'$44th = "Brown-2" {print$0} 'grade.txt
M.Tansley 05/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 Brown-3 12 26 26
# less than
[root@wuke shell] # awk'$6
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.