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 use the awk command in Linux

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How do I use the awk command in Linux? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Introduction to awk

Awk gets its name from the initials of its founders Alfred Aho, Peter Weinberger and Brian Kernighan. In fact, AWK does have its own language, the AWK programming language, which the three creators have officially defined as a "style scanning and processing language". It allows you to create short programs that read input files, sort data, process data, perform calculations on input, generate reports, and countless other functions.

Awk is a great language, it is suitable for text processing and report generation, its syntax is more common, drawing on some of the essence of some languages, such as C language. In the daily work of linux system, play a very important role, mastering awk will make your work higher and higher. Awk is the boss of the three swordsmen. If the sword comes out of the scabbard, it will be extraordinary.

Usage

Awk'{pattern + action}'{filenames}

Although the operation can be complex, the syntax is always like this, where pattern represents what AWK looks for in the data, and action is a series of commands that are executed when a match is found. Curly braces ({}) do not need to appear all the time in the program, but they are used to group a series of instructions according to a specific pattern. Pattern is the regular expression that you want to represent, surrounded by diagonal bars.

The most basic function of awk language is to browse and extract information from files or strings based on specified rules. After awk extracts information, other text operations can be carried out. A complete awk script is usually used to format information in a text file.

Typically, awk processes units as a behavior of a file. Awk receives each line of the file and then executes the appropriate command to process the text.

The principle of awk

Through a short command, we will understand how it works.

[root@Gin scripts] # awk'{print $0}'/ etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin... [root@Gin scripts] # echo hhh | awk'{print "hello,world"} 'hello,world [root@Gin scripts] # awk' {print "hiya"}'/ etc/passwdhiyahiyahiyahiya...

You will see the contents of the / etc/passwd file appear in front of you. Now, explain what awk has done. When we call awk, we specify / etc/passwd as the input file. When awk is executed, it executes the print command on each line in / etc/passwd in turn.

All the output is sent to stdout, and the result is exactly the same as executing cat / etc/passwd.

Now, explain the {print} code block. In awk, curly braces are used to put pieces of code together, similar to the C language. There is only one print command in the code block. In awk, if only the print command appears, the entire contents of the current line will be printed.

Again, awk executes this script for every line in the input file.

$awk-F ":"'{print $1}'/ etc/passwd$ awk-F ":"'{print $1 $3}'/ etc/passwd$ awk-F ":"'{print $1 "" $3}'/ etc/passwd$ awk-F ":" {print "username:" $1 "\ t\ tuid:" $3 "}'/ etc/passwd

-F parameter: specify the delimiter, you can specify one or more

Do string concatenation after print

Here are a few examples to understand how awk works:

Example 1: view only the contents of lines 20 to 30 in the test.txt file (100 lines) (corporate interview)

[root@Gin scripts] # awk'{if (NR > = 20 & & NR2&&b > 1) | | b > 1)}'

0 1

Judge whether the expression a > 2 is true or false, and the following expressions are the same.

Awk regular operator:

[root@Gin scripts] # awk 'BEGIN {a = "100testaa"; if (aura ok 100 /) {print "ok"}'

Ok

[root@Gin scripts] # echo | awk 'BEGIN {a = "100testaaa"} a~/test/ {print "ok"}'

Ok

Relational operator:

Such as: >

< 可以作为字符串比较,也可以用作数值比较,关键看操作数如果是字符串就会转换为字符串比较。两个都为数字 才转为数值比较。字符串比较:按照ascii码顺序比较。 [root@Gin scripts]# awk 'BEGIN{a="11";if(a>

= 9) {print "ok"}'# No output [root@Gin scripts] # awk 'BEGIN {print "ok"} if (a > = 9) {print "ok"}}' ok [root@Gin scripts] # awk 'BEGIN {print "ok"}}' ok

Awk arithmetic operator:

Note that all operands are used as arithmetic operators, operands are automatically converted to numeric values, and all non-numeric values become 0.

[root@Gin scripts] # awk 'BEGIN {a = "b"; print axioms / print axioms / print /

The axiom +, + + a here is the same as the javascript language: axiom + is the first assignment plus + +; + + an is the first + + and then the assignment.

Ternary operator?:

[root@Gin scripts] # awk 'BEGIN {a = "b"; print asides = "b"? "ok": "err"}' ok [root@Gin scripts] # awk 'BEGIN {a = "b"; print asides = "c"? "ok": "err"}' err

Common awk built-in variables

Note: there are many built-in variables, please refer to the relevant information.

Field delimiter FS

FS= "\ t" one or more Tab separations

[root@Gin scripts] # cat tab.txtww CC IDD [root@Gin scripts] # awk 'BEGIN {FS= "\ t +"} {print $1

FS= "[: space:] +]" one or more white space, default

[root@Gin scripts] # cat space.txtwe are studing awk now! [root@Gin scripts] # awk-F [[: space:] +]'{print $1, 2, 3, 4, 5} 'space.txtwe are [root@Gin scripts] # awk-F [[: space:] +]' {print $1, space.txtwe are 2}'

FS= "[":] + "is separated by one or more spaces or:

[root@Gin scripts] # cat hello.txtroot:x:0:0:root:/root:/bin/bash [root@Gin scripts] # awk-F ["":] +'{print $1 hello.txtroot 3} 'hello.txtroot x 0

Number of fields NF

[root@Gin scripts] # cat hello.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin:888 [root@Gin scripts] # awk-F ":" 'NF==8 {print $0}' hello.txtbin:x:1:1:bin:/bin:/sbin/nologin:888

Number of records NR

[root@Gin scripts] # ifconfig eth0 | awk-F ["":] + 'NR==2 {print $4}' # # NR==2, that is, line 2

192.168.17.129

RS record delimiter variable

Set FS to "\ n" to tell awk that each field occupies a row. By setting RS to "", it also tells awk that each address record is separated by a blank line.

[root@Gin scripts] # cat recode.txtJimmy the Weasel100 Pleasant DriveSan Francisco,CA 123456Big Tony200 Incognito Ave.Suburbia,WA 64890 [root@Gin scripts] # cat awk.txtBEGIN [FS= "\ n" RS= ""} {print $1 "," $2 "," $3} [root@Gin scripts] # awk-f awk.txt recode.txtJimmy the Weasel,100 Pleasant Drive,San Francisco,CA 123456Big Tony,200 Incognito Ave.,Suburbia,WA 64890 "

OFS output field delimiter

[root@Gin scripts] # cat hello.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin:888 [root@Gin scripts] # awk 'BEGIN {FS= ":"} {print $1 "," $2 "," $3}' hello.txtroot,x,0bin,x,1 [root@Gin scripts] # awk 'BEGIN {FS= ":"; OFS= "#"} {print $1

ORS output record delimiter

[root@Gin scripts] # cat recode.txtJimmy the Weasel100 Pleasant DriveSan Francisco,CA 123456 Big Tony200 Incognito Ave.Suburbia,WA 64890 [root@Gin scripts] # cat awk.txtBEGIN {FS= "\ n" RS= "ORS="\ n\ n "} {print $1", "$2", "$3} [root@Gin scripts] # awk-f awk.txt recode.txtJimmy the Weasel,100 Pleasant Drive,San Francisco,CA 123456 Big Tony,200 Incognito Ave.,Suburbia,WA 64890

Awk regularity

Regular application

Regular expression

Awk'/ REG/ {action} 'file,/REG/ is a regular expression. You can send the records that meet the conditions in $0 to: action for processing.

[root@Gin scripts] # awk'/ root/ {print $0} 'passwd # # matches all lines containing root root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin [root@Gin scripts] # awk-F:' $5~/root/ {print $0} 'passwd # # delimited by semicolons Matches the line root:x:0:0:root:/root:/bin/bash [root@Gin scripts] # ifconfig eth0 where the fifth field is root | awk 'BEGIN {FS= "[[: space:]:] +"} NR==2 {print $4}' 192.168.17.129

Boolean expression

Awk 'Boolean expression {action}' awk executes the code block only if the previous Boolean expression evaluates to true.

[root@Gin scripts] # awk-F:'$1 million = "root" {print $0} 'passwdroot:x:0:0:root:/root:/bin/bash [root@Gin scripts] # awk-F:' ($1 million = "root") & & ($5 million = "root") {print $0} 'passwdroot:x:0:0:root:/root:/bin/bash

If, loops, and arrays of awk

Conditional statement

Awk provides a very good if statement similar to the C language.

{if ($1 million = "foo") {if ($2 million = "foo") {print "uno"} else {print "one"}} elseif ($1 million = "bar") {print "two"} else {print "three"}}

Using the if statement, you can also change the code:

! / matchme/ {print $1 $3 $4}

Convert to:

{if ($0! ~ / matchme/) {print $1 $3 $4}}

Cyclic structure

We have seen the while loop structure of awk, which is equivalent to the corresponding C language while loop. Awk also has a "do...while" loop, which evaluates the condition at the end of the code block, rather than at the beginning of the standard while loop.

It is similar to the "repeat...until" loop in other languages. The following is an example:

Do...while example

{count=1do {print "I get printed at least once no matter what"} while (count! = 1)}

Unlike a normal while loop, the "do...while" loop is always executed at least once because the condition is evaluated after the code block. In other words, when a normal while loop is encountered for the first time, the loop will never be executed if the condition is false.

For cycle

Awk allows you to create for loops, which are like while loops and equivalent to C language for loops:

For (initial assignment; comparison; increment) {code block}

The following is a short example:

For (Xero1th x20) {break} Xerox +}

This code prints "iteration1" to "iteration21", except for "iteration4". If the iteration equals 4, increase x and call the continue statement, which immediately causes awk to start the next loop iteration without executing the rest of the code block. Just like break.

The continue statement is suitable for various awk iteration loops. When used in the body of a for loop, continue automatically increases the loop control variables. The following is an equivalent loop:

For (xylene 1)

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