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 > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Awk:
Test template:
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
P.Bunnyaaa 02/99 48 Yellow 12 35 2658
J.Troll 07/99 4842 Brown-3 12 26 266666
L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
Modes and actions:
The most common action is printing (print), which also has longer action codes if and while and for
The pattern can be any conditional statement or compound statement or regular expression also includes two special fields BEGIN and END.
BEGIN is used before awk formally reads the text, while END is used after awk has done all the operations.
The input method of the awk script:
Script name (test.awk) file name (abc.txt) formal use method is method 1
Method 1: test.awk abc.txt
Method 2: test.awk
< abc.txt 方法三:abc.txt | test.awk awk使用时出现错误的原因: 确保整个awk命令用单引号括起来。 确保命令内所有引号成对出现。 确保用花括号括起动作语句,用圆括号括起条件语句。 可能忘记使用花括号,也许你认为没有必要,但awk不这样认为,将按之解释语法。 NR:表示awk开始执行程序后所读取的数据行数 例:[root@localhost awk]# awk '{print NR}' data.f 1 2 3 4 5 释:也可以在花括号前加入END字段,表示打印最后一行行数。 $NF:最后一列数据 例:[root@localhost awk]# awk '{print $NF}' data.f 44.44 2678. 2658 266666 44544 释:可以在花括号前加入END,表示打印最后一行的最后一列。 保存输出: 例:[root@localhost awk]# awk '{print $0}' data.f |tee wow M.Tansley 05/99 48311 Green 8 40 44.44 J.Lulu 06/99 48317 green 9 24 2678. P.Bunnyaaa 02/99 48 Yellow 12 35 2658 J.Troll 07/99 4842 Brown-3 12 26 266666 L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544 释:"$0"打印全行,tee是将管道符前面的结果保存到wow文件中同时标准输出在屏幕上。 打印报告头和结束尾: 例:[root@localhost awk]# awk 'BEGIN {print "name color\n------------------------"} {print $1,$4}' data.f name color ------------------------ M.Tansley Green J.Lulu green P.Bunnyaaa Yellow J.Troll Brown-3 L.Tansleyaaa Brown-2 释:"\n"另起一行 例:[root@localhost awk]# awk 'BEGIN {print "name color\n---------------"} {print $1,$4} END {print "\n-----------jieshu"}' data.f name color -------------------- M.Tansley Green J.Lulu green P.Bunnyaaa Yellow J.Troll Brown-3 L.Tansleyaaa Brown-2 ---------------jieshu [root@localhost awk]# awk 'BEGIN {print "name\tcolor"} {print $1"\t" $2}' data.f name color M.Tansley 05/99 J.Lulu 06/99 P.Bunnyaaa 02/99 J.Troll 07/99 L.Tansleyaaa 05/99 释:"\t"执行tab键 正则表达式: 在awk中正则表达式是使用斜线来表示的,例如,查找Green字符可以这样表达"/Green/",在awk中可以使用的元字符有如下; \ :转意符号,如果在命令中有特殊符号,但是不想用特殊符号的功能只需要在特殊符号前加转义符号。 . :一位任意数 [] :为查询大小写信息. () :使用条件语句需要用圆括号。 * :多位任意数。 ^ :匹配模式头部分 $ :匹配模式尾部分 | :或 ? :匹配可能出现的字符 + :使用+匹配一个或多个字符 条件操作符: >Less than
Example: [root@localhost awk] # awk'$3
< "48" {print $0}' data.f L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544 例:[root@localhost awk]# awk '{if($6 >$7) print $1 "\ tok"} 'data.f.bak
J.Troll ok
=: greater than or equal to
Example: [root@localhost awk] # awk'$3 > = "48" {print $0} 'data.f
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
P.Bunnyaaa 02/99 48 Yellow 12 35 2658
J.Troll 07/99 4842 Brown-3 12 26 266666
~: match regular expression
Example: [root@localhost awk] # awk'{if ($4~/Brown/) print $0} 'data.f
J.Troll 07/99 4842 Brown-3 12 26 266666
L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
Example: [root@localhost awk] # awk'$0 ~ / Brown/' data.f
J.Troll 07/99 4842 Brown-3 12 26 266666
L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
! ~: does not match regular expressions
Example: [root@localhost awk] # awk'$0! ~ / Brown/' data.f
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
P.Bunnyaaa 02/99 48 Yellow 12 35 2658
Example: [root@localhost awk] # awk'{if ($4! ~ / Brown/) print $0} 'data.f
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
P.Bunnyaaa 02/99 48 Yellow 12 35 2658
Any character match:
Example: [root@localhost awk] # awk'$0,000,000 / ^... a / 'data.f
M.Tansley 05/99 48311 Green 8 40 44.44
L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
The first three digits of the match are any character and the fourth digit is a field.
The use of "or":
Example: [root@localhost awk] # awk'$0 ~ / (Brown | gree) / 'data.f
J.Lulu 06/99 48317 green 9 24 2678.
J.Troll 07/99 4842 Brown-3 12 26 266666
L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
For example, [root@localhost awk] # awk'$0 ~ / (Brown | [Gg] ree) / 'data.f
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
J.Troll 07/99 4842 Brown-3 12 26 266666
L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
The first line:
Example: [root@localhost awk] # awk'/ ^ J Universe 'data.f
J.Lulu 06/99 48317 green 9 24 2678.
J.Troll 07/99 4842 Brown-3 12 26 266666
Match the pattern:
&: both sides are true before the message can be printed. (valid for one row only)
For example, [root@localhost awk] # awk'{if ($3 pound 48 / & & $4 million / [Gg] reen/) print $0} 'data.f
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
| |: one side is true or all is true |
Example: [root@localhost awk] # awk'{if ($0~/Brown/ | | $4 million / [Gg] reen/) print $0} 'data.f
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
J.Troll 07/99 4842 Brown-3 12 26 266666
L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
Awk built-in variables:
NR: number of file lines
NF: number of domain blocks
FILENAME: the specified file name
Example: [root@localhost awk] # awk'{print NR,NF,$0} END {print FILENAME} 'data.f
1 7 M.Tansley 05/99 48311 Green 8 40 44.44
2 7 J.Lulu 06/99 48317 green 9 24 2678.
3 7 P.Bunnyaaa 02/99 48 Yellow 12 35 2658
4 7 J.Troll 07/99 4842 Brown-3 12 26 266666
5 7 L.Tansleyaaa 05/99 4712 Brown-2 12 30 44544
Data.f
Example: [root@localhost awk] # echo $PWD | awk-F "/"'{print $NF}'
Awk
Set the input field to domain variable name:
Example: [root@localhost awk] # awk'{name=$1;color=$4;if (color~/Ye/) print name "\ tok"} 'data.f
P.Bunnyaaa ok
Note: pay attention to the use of semicolons, which separate awk commands.
Assign a value to a variable:
Example: [root@localhost awk] # awk'{if ($6 < "27") print $0} 'data.f
J.Lulu 06/99 48317 green 9 24 2678.
J.Troll 07/99 4842 Brown-3 12 26 266666
Example: [root@localhost awk] # awk 'BEGIN {shuzi= "27"} {if ($6 < shuzi) print $0}' data.f
J.Lulu 06/99 48317 green 9 24 2678.
J.Troll 07/99 4842 Brown-3 12 26 266666
Explanation: it is usually useful to assign values in the BEGIN section, which can reduce a lot of hassle when making changes to awk expressions.
Modify the value of the numerical range:
Example: [root@localhost awk] # awk'{if ($1pm = "M.Tansley") $6pm 6-1th print $1pm 6m 7} 'data.f
M.Tansley 39 44.44
J.Lulu 24 2678.
P.Bunnyaaa 35 2658
J.Troll 26 266666
L.Tansleyaaa 30 44544
Interpretation: M.Tansley dropped from 40 to 39.
Example: [root@localhost awk] # awk'{if ($1 million) $6 million (6-1) print $1 (6) 7} 'data.f
M.Tansley 39 44.44
J.Lulu 23 2678.
P.Bunnyaaa 34 2658
J.Troll 25 266666
L.Tansleyaaa 29 44544
Explanation: all figures are reduced by one.
Modify the text field:
Example: [root@localhost awk] # awk'{if ($1 = "P.Bunnyaaa") ($1 = "P.Bunnyaaa.abcaaa"); print $1} 'data.f
M.Tansley
J.Lulu
P.Bunnyaaa.abcaaa
J.Troll
L.Tansleyaaa
Explanation: remember to use the two-second sign (") in the string. Don't forget the semicolon (;) and enclose the entire syntax in parentheses.
Only change records are displayed:
Example: [root@localhost awk] # awk'{if ($1 M.Tansley = "M.Tansley") {$1 = "M.Tansley.abc"; print $1}} 'data.f
M.Tansley.abc
Example: [root@localhost awk] # awk'{if ($1 M.Tansley = "M.Tansley") {$1 = "M.Tansley.abc"; print $0}} 'data.f
M.Tansley.abc 05/99 48311 Green 8 40 44.44
Note: note the position of the curly braces
Create a new output domain:
Example: [root@localhost awk] # awk 'BEGIN {print "name\ t fenzhi"} {if ($5 < $6) {$8 data.f
Name fenzhi
M.Tansley 32
J.Lulu 15
P.Bunnyaaa 23
J.Troll 14
L.Tansleyaaa 18
Explanation: print a header before outputting the result, and then compare the value, "$8 million 6 color 5" which is an assignment to $8. Remember to use the format of syntax.
Example: [root@localhost awk] # awk 'BEGIN {print "name\ t shuzi"} {if ($5 < $6); diff=$6-$5;print $1 Diff}' data.f
Name shuzi
M.Tansley 32
J.Lulu 15
P.Bunnyaaa 23
J.Troll 14
L.Tansleyaaa-90
Explanation: you can also use domain variable names.
The file length is added:
Example: [root@localhost awk] # ll-h | awk'/ ^ [^ d] / {print $8 "\ t" $5} {tol+=$5} END {print "zongshu:" tol}'
Data.f 217
Data.f.bak 201
Zongshu:422
Explanation: there is a deviation in the sum of the numerical addition, which should be caused by the error caused by the unit conversion. Check at a later stage.
Replace (gsub):
Example: [root@localhost awk] # awk 'gsub (/ Green/, "abcd") {print $0}' data.f
M.Tansley 05/99 48311 abcd 8 40 44.44
Explanation: replace one string with another in the entire record, using regular expression format, / target pattern /, replace pattern
Awk simple script:
Example: [root@localhost awk] # cat jiaoben
#! / bin/awk-f
#
BEGIN {
Print "hello"
Print "chengjiruxia"
Print "= ="
}
(tot+=$6)
END {print "zongshu:" tot
Print "pingjunshu" tot/NR}
Explanation: to add permissions to the awk script, chmod Ubunx jiaoben, you cannot use sh jiaoben data.f in this way.
Explanation: tot/NR is the total number of tot divided by rows (NR)
Example: [root@localhost awk] #. / jiaoben data.f
Hello
Chengjiruxia
= =
M.Tansley 05/99 48311 Green 8 40 44.44
J.Lulu 06/99 48317 green 9 24 2678.
P.Bunnyaaa 02/99 48 Yellow 12 35 2658
J.Troll 07/99 4842 Brown-3 12 26 266666
L.Tansleyaaa 05/99 4712 Brown-2 120 30 44544
Zongshu:155
Pingjunshu31
Example: [root@localhost awk] # awk 'BEGIN {OFS= ":"} {print $1 recorder 3 meme 4}' digresult.txt
News.sina.com.cn:111.12.251.212:111.12.251.203
Jupiter.sina.com.cn:111.12.251.217:111.12.251.212
Cmnetnews.sina.com.cn:111.12.251.219:111.12.251.217
Us.sina.com.cn:111.12.251.212:111.12.251.203
Www.sina.com:111.12.251.212:111.12.251.203
Explanation: the printed results are separated by colons, also known as output delimiters.
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.