In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is to share with you about the operation of awk, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Awk is a programming language for dealing with text and pattern matching. With sed and grep, commonly known as the three Musketeers under Linux. Learning awk means that you have another option for dealing with text on the Linux command line. This article focuses on teaching you how to use it. After reading this article, you will have a general idea of how to use it, and strive to make it easy to use.
Terminology matting
In awk's text processing rules, awk treats text files as a text database of fields and records. By default, awk treats each row as a record, which means that the delimiter of the record is that the delimiter of the record can be changed through the built-in variable RS.
In each record, the record is divided into several fields, that is, the record is made up of fields, and the default delimiter of the field is a space or tab.
I. basic usage
Like the Linux command we usually use, awk follows a certain format, as follows:
# use format
Event file executed by awk
# for example:
Root@jaking-virtual-machine:~# awk'{print $0} 'test.txtMy first language:PythonMy second language:ShellMy third language:JavaMy fourth language:C
Where print represents printing, $0 represents an entire record, and test.txt represents a file. So
Awk'{print $0} 'test.txt
It means to print out every line of record in the test.txt file.
$0 represents the entire record, but $1, $2, $3. Represents the first field and the second field in the entire record.
Root@jaking-virtual-machine:~# awk'{print $1} 'test.txtMyMyMyMyroot@jaking-virtual-machine:~# awk' {print $2} 'test.txtfirstsecondthirdfourthroot@jaking-virtual-machine:~# awk' {print $3} 'test.txtlanguage:Pythonlanguage:Shelllanguage:Javalanguage:C
We just said that the default delimiters for fields are spaces or tabs, which means that we can explicitly specify the delimiter ourselves. Let's use ":" as our delimiter.
Root@jaking-virtual-machine:~# awk-F':'{print $2} 'test.txtPythonShellJavaC
Above we specify our delimiter with the parameter-F, that is, if you want to specify the delimiter of the field, you can specify the delimiter with the parameter-F.
II. Conditional restrictions
When printing text, we can specify some conditions. The format is as follows:
Action file to be performed by awk parameter condition
For example, we specify a record with a delimiter of ":" on the condition that the second field is "Java".
# print the text with the second field "Java"
Root@jaking-virtual-machine:~# awk-F':'$2 = = "Java" {print $2} 'test.txtJava
Print the second field of the odd line:
# print records of odd lines
Root@jaking-virtual-machine:~# awk-F':'NR% 2 = = 1 {print $2} 'test.txtPythonJava
Where NR is a built-in variable that represents the record currently being processed, that is, the number of records the current record is.
III. Conditional statement
Like our usual programming, awk also provides conditional statements such as if, else, while, and so on.
For example, print the second and subsequent records:
Root@jaking-virtual-machine:~# awk'{if (NR > 1) print $2} 'test.txtsecondthirdfourth
Notice that the above field delimiter is a space, and the if statement is specified in "{}".
Look at another example:
Root@jaking-virtual-machine:~# awk'{if ($1)
< "s") print $1; else print $2}' test.txt# 如果第一个字段小于"s",则打印第一个字段,否则打印第二个字段MyMyMyMyroot@jaking-virtual-machine:~# awk '{if($1 >"s") print $1; else print $2} 'test.txtfirstsecondthirdfourthroot@jaking-virtual-machine:~# awk' {if ($1)
< "l") print $1; else print $2}' test.txtMyMyMyMyroot@jaking-virtual-machine:~# awk '{if($1 >"l") print $1; else print $2} 'test.txtfirstsecondthirdfourthroot@jaking-virtual-machine:~# awk' {if ($1 > "c") print $1; else print $2} 'test.txtfirstsecondthirdfourthroot@jaking-virtual-machine:~# awk' {if ($1 > "d") print $1; else print $2} 'test.txtfirstsecondthirdfourthroot@jaking-virtual-machine:~# awk' {if ($1 > "p") print $1; else print $2} 'test.txtfirstsecondthirdfourthroot@jaking-virtual-machine:~# awk' {if ($1 < "p") print $1 Else print $2} 'test.txtMyMyMyMyroot@jaking-virtual-machine:~# awk' {if ($3 < "s") print $3; else print $2} 'test.txtlanguage:Pythonlanguage:Shelllanguage:Javalanguage:Croot@jaking-virtual-machine:~# awk' {if ($2 < "s") print $3; else print $2} 'test.txtlanguage:Pythonsecondthirdlanguage:Croot@jaking-virtual-machine:~# awk' {if ($2 < "s") print $1; else print $2} 'test.txtMysecondthirdMy
Fourth, function
Awk provides some built-in functions for us to use. The common functions are as follows:
Tolower (): the character is converted to lowercase. Toupper (): character to uppercase length (): returns the length of the string. Substr (): returns a substring. Sqrt (): square root. Rand (): random number. Root@jaking-virtual-machine:~# awk'{print toupper ($1)} 'test.txtMYMYMYMYroot@jaking-virtual-machine:~# awk' {print tolower ($1)} 'test.txtmymymymyroot@jaking-virtual-machine:~# awk-F':'{print toupper ($2)} 'test.txtPYTHONSHELLJAVACroot@jaking-virtual-machine:~# awk-F':'{print tolower ($2)} 'test.txtpythonshelljavac
5. Variables
We just said that NR is a built-in variable that indicates the number of records currently being processed. The common built-in variables are as follows:
NR: indicates the number of lines currently being processed NF: indicates how many fields there are on the current line FILENAME: current file name FS: field delimiter, default is space and tab. RS: line separator, which is used to split each line. The default is the newline character. OFS: the delimiter of the output field, which is used to separate fields when printing. The default is a space. ORS: the delimiter of the output record, which is used to print the time delimited record. The default is the newline character.
For example, if we want to print the last field of each record, we can use the variable NF.
Root@jaking-virtual-machine:~# awk'{print $NF} 'test.txtlanguage:Pythonlanguage:Shelllanguage:Javalanguage:C
By the way, the NR variable is also quite useful, for example:
Root@jaking-virtual-machine:~# awk'{print NR "." $0} 'test.txt1. My first language:Python2. My second language:Shell3. My third language:Java4. My fourth language:C above is what the operation of awk is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, 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.
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.