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

Linux awk can understand at a glance.

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Linux awk can understand at a glance.

What is http://www.cnblogs.com/wangqiguo/p/5863266.html#_label12awk?

Awk is a command line tool under the linux environment, but because of the powerful ability of awk, we can pass a string for the awk tool, the content of the string is similar to the syntax of a programming language, we can call it the Awk language, and the awk tool itself can be regarded as the parser of the Awk language. It's like the relationship between the python parser and the Python language. What do we usually do with awk, and what is awk suitable for? Because awk naturally provides to deal with the text in a document, so if each line in a document is separated by a specific separator (usually a space), we can think of the file as composed of many columns of text, which is most suitable for processing with awk. In fact, awk is often used to deal with log documents, do some statistical work and so on.

General composition of the awk command

The most common job of awk is to iterate through each line of a file, and then process each line of the file separately. A complete awk command looks like this:

Awk [options] 'BEGIN {commands} pattern {commands} END {commands}' file

Where options represents the optional command-line option for awk, perhaps the most common of which is-F, which specifies a delimiter that separates each line in the file into columns. All the content in the following single quotation marks is the program script of awk, and awk needs to process each column after each line of the file is split. File is the name of the file that awk processes. Let's experience the function of awk through demo.

Awk splits each row

Echo'11 22 33 44'| awk'{print $3 "" $2 "$1}'

Output: 33 22 11

We pipe the string 11 22 33 44 to the awk command, equivalent to awk processing a file, the content of the file is 11 22 33 44 above the command we did not add-F specified split symbol, in fact, by default awk uses spaces to split each line, if you need to specify other characters, use-F display specified. The above command is to divide 11 22 33 44 into 4 columns through spaces (no matter how many spaces are treated as a space between columns). In awk, there is a variable referenced by the number $, which refers to the contents of each column divided in the current row, with the number starting from 1, for example, $1 for the contents of the first column, $2 for the second column, and so on. $0 represents the contents of the current entire line. Print is a built-in function of awk that prints out the value of a variable. We added spaces enclosed in double quotes between $3 $2 $1, and if not, the values of these variables are printed out and linked together. The content of {} in the awk command here is actually the middle part of the complete mode above us. We omitted the BEGIN block, the END block, and the middle program block, and we also omitted the pattern part, that is, if we do not add BEGIN or END instructions, then the program block is the middle program block in the complete mode above, and the middle program block performs the operation of looping through every line of the file content. If the file has 10 lines, the middle block will be run 10 times, each time processing the contents of one line, and after processing the current line, the next loop will automatically process the next line in turn.

Let's see what happens if there are two columns, for example:

Echo-e'11 22 33 44\ naa bb cc dd' | awk'{print $3 "" $2 "" $1}'

Output:

33 22 11

Cc bb aa

Note that the purpose of the echo command using the-e option is to keep the\ nformat in the string valid, otherwise the newline will be ignored. So how is the above command executed? let's simulate the execution of awk. First, awk reads the contents of the first line, separates the columns in that line with spaces, and assigns the string 11 to $1, 22, to $2, 33, to $3, 44, to $4. And then print it out through print. Then read the contents of the second line and perform the same operation above.

Use the parttern section

Now that we've learned the simplest commands of awk, let's add a little bit more to it, adding the pattern section in front of the block, for example:

Echo-e'1 2 3 4\ n5 6 7 8'| awk'$1 > 2 {print $3 "" $2 "$1}'

Output: 7 6 5

This program is almost the same as the above program, except that we add $1 > 2 in front of the block to indicate that if the value of the first column of the current row is greater than 2, the current row is processed, otherwise it is not processed. To put it bluntly, the pattern part is used to filter out the lines that need to be processed from the file for processing, and if not, cycle through all the lines in the file. The pattern part can be the result of any conditional expression, such as >, =

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

Servers

Wechat

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

12
Report