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

What are the special ways to use awk

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

Share

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

Editor to share with you what the special use of awk, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Awk is a very good language in Unix environment, which is suitable for text processing and report generation. It also has many well-designed features that allow for special skill programming.

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:

# event files executed using the format 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.

It means the whole record, but 1, 3... .. Then it represents the first field and the second field in the whole 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 # if the first field is less than "s", print the first field, otherwise print the second field MyMyMyMyroot@jaking-virtual-machine:~# awk'{if ($1 > "s") print $1; else print $2} 'test.txtfirstsecondthirdfourthroot@jaking-virtual-machine:~# awk' {if ($1 MyMyMyMyroot@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 MyMyMyMyroot@jaking-virtual-machine:~# awk'{if ($3 language:Pythonlanguage:Shelllanguage:Javalanguage:Croot@jaking-virtual-machine:~# awk'{if) ($2 language:Pythonsecondthirdlanguage:Croot@jaking-virtual-machine:~# awk'{if ($2 MysecondthirdMy IV)

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 V, variable

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

These are all the contents of this article entitled "what are the special uses of awk?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

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

12
Report