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

How to understand the command line programming tool Awk under Linux

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

Share

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

This article mainly introduces "how to understand the command line programming tool Awk under Linux". In daily operation, I believe many people have doubts about how to understand the command line programming tool Awk under Linux. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to understand the command line programming tool Awk under Linux". Next, please follow the editor to study!

What is Awk?

Awk is a small programming language and command line tool. (its name comes from the initials of its founders Alfred Aho, Peter Weinberger and Brian Kernighan.) It is ideal for log processing on the server, mainly because Awk can manipulate files, usually building lines in readable text.

I say it works for servers because log files, dump files (dump files), or any text format of the server terminating the dump to disk will become very large, and you will have a large number of such files on each server. If you've ever had to analyze a few gigabytes of files on 50 different servers without Splunk or other equivalent tools, you'll think it's a bad thing to get and download all of these files and analyze them.

I have personally experienced this situation. When some Erlang nodes are about to die and leave a 700MB-to-4GB crash dump (crash dump), or when I need to quickly browse the log on a small personal server (called VPS) to find a regular pattern.

In any case, Awk is not just for finding data (otherwise, grep or ack is enough)-it also enables you to process and transform data.

Code structure

The code structure of the Awk script is simple: a series of patterns (pattern) and behaviors (action):

The code is as follows:

# comment

Pattern1 {ACTIONS;}

# comment

Pattern2 {ACTIONS;}

# comment

Pattern3 {ACTIONS;}

# comment

Pattern4 {ACTIONS;}

When scanning each line of a document, you must match and compare each pattern, and match only one pattern at a time. So, if I give a file that contains the following:

The code is as follows:

This is line 1

This is line 2

The line this is line 1 will match the Pattern1. If the match succeeds, the ACTIONS is executed. Then this is line 1 matches Pattern2. If the match fails, it jumps to Pattern3 to match, and so on.

Once all the patterns have been matched, this is line 2 will match them with the same steps. The same is true for other lines until the entire file is read.

In short, this is how Awk works.

Data type

Awk has only two main data types: strings and numbers. Even so, strings and numbers in Awk can be converted to each other. A string can be interpreted as a number and its value converted to a numeric value. If the string does not contain a number, it is converted to 0.

They can all use the = operator to assign values to variables in the ACTIONS section of your code. We can declare and use variables at any time, anywhere, or we can use uninitialized variables, where their default value is an empty string: ".

Finally, Awk has array types, and they are dynamic one-dimensional associative arrays. Their syntax goes like this: var[ key] = value. Awk can simulate multidimensional arrays, but anyway, this is a big trick (big hack).

Pattern

The patterns you can use fall into three main categories: regular expressions, Boolean expressions, and special patterns.

Regular and Boolean expressions

The Awk regular expressions you use are relatively lightweight. They are not PCRE under Awk (but gawk can support this library-depending on the implementation! Please use awk

-version view), however, it is sufficient for most usage requirements:

The code is as follows:

/ admin/ {...} # any line that contains' admin'

/ ^ admin/ {.} # lines that begin with 'admin'

/ admin$/ {...} # lines that end with 'admin'

/ ^ [0-9.] + / {.} # lines beginning with series of numbers and periods

/ (POST | PUT | DELETE) / # lines that contain specific HTTP verbs

Note that the pattern cannot capture specific groups (groups) to execute in the ACTIONS portion of the code. Patterns are designed to match content.

Boolean expressions are similar to Boolean expressions in PHP or Javascript. In particular, the & & ("and"), | | ("or"),! ("non") operators can be used in awk. You can find them in almost all C-like languages. They can operate on regular data.

A feature that is more similar to PHP and Javascript is the comparison operator, = =, which performs fuzzy matching (fuzzy matching). So the "23" string equals 23, and the "23" = = 23 expression returns true. The! = operator is also used in awk, and don't forget other common operators: >, =, and = threshold {flag=1; ct=$4}

/ ^ Message queue length: / & & $4 < threshold {flag=0}

# Program counter: 0x00007f5fb8cb2238 (io:wait_io_mon_reply/2 + 56)

# 1 2 3 4 5 6

Flag = = 1 & & / ^ Program counter: / {print ct ":", substr ($4jue 2)}

At this point, the study on "how to understand the command line programming tool Awk under Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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