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

File processor-- awk

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

Share

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

1. Brief introduction to awk:

Awk is a programming language used to process text and data under linux/unix. Data can come from standard input, one or more files, or the output of other commands. It supports advanced functions such as user-defined functions and dynamic regular expressions, and is a powerful programming tool under linux/unix. It is used on the command line, but more often as a script.

The way awk handles text and data is that it scans the file line by line, from the first line to the last line, looking for lines that match a particular pattern, and doing what you want on those lines. If no processing action is specified, the matching lines are displayed to standard output (screen), and if no mode is specified, the specified rows of all operations are processed.

Two formal grammatical formats of awk

Awk [options] 'commands' filenames / / filter and output eligible content of the file awk [options]-f awk-script-file filenam / / invoke the edit instruction from the script, filter and output the content

Options:

-F defines the input field delimiter. The default delimiter is a space or tab (tab)

Commands

BEGIN {} END {} row processing before row processing BEGIN {} is usually used to define variables such as BEGIN {FS= ":"; OFS= "- -"}

Use the begin statement to set the count and print head. The BEGIN statement is used before any text browsing action, after which the text browsing action starts to execute based on the input text, and the END statement is used to print out the total number of text and the ending status after the awk completes the text browsing action.

Awk command format

Awk 'pattern' filename example: awk-F:' / root/' / etc/passwd awk'{action} 'filename example: awk-F:' {print $1}'/ etc/passwd awk 'pattern {action}' filename example: awk-F:'/ root/ {print $1 camera 3}'/ etc/passwd example: awk 'BEGIN {FS= ":"} / root/ {print $1 $3}'/ etc/passwdcommand | awk 'pattern {action}' example: df-P | grep'/'| awk'$4 > 25000 {print $4}'

Third, the working principle of awk

# awk-F:'{print $1 recorder 3}'/ etc/passwd

(1) awk takes a line as input and assigns this line to the internal variable $0. Each line can also be called a record, ending with a newline character.

(2) the row is then broken down into fields (or fields) by: (the default is spaces or tabs), each stored in a numbered variable, up to 100 fields starting at $1

(3) how does awk know to separate fields with spaces? Because there is an internal variable FS to determine the field delimiter. Initially, FS is assigned a space

(4) when awk prints fields, it will print using the print function as set, and awk will add a space between the printed fields because there is a comma between $1 and 3. The comma is special because it maps to another internal variable called the output field delimiter OFS,OFS defaults to a space

(5) after awk output, another line will be taken from the file and stored in $0, overwriting the original content, and then the new string will be separated into fields and processed. The process continues until all lines have been processed

4. Awk built-in variables

FS: specifies the field separator for each line of text, which defaults to spaces or tab stops. (- F) NF: the number of fields in the currently processed row. NR: the line number (ordinal) of the currently processed row. $0: the entire line content of the currently processed row. $n: the nth field (nth column) of the currently processed row. FILENAME: the name of the file being processed. RS: data records are separated. The default is\ n, that is, one record per behavior.

5. Formatted output

Print function

# date | awk'{print "Month:" $2 "\ nYear:" $NF}'# awk-F:'{print "username is:" $1 "\ t uid is:" $3}'/ etc/passwd# awk-F:'{print "\ tusername and uid:" $1jue 3 "!"}'/ etc/passwd

Printf function

# awk-F:'{printf "%-15s%-10s%-15s\ n", $1meme / etc/passwd# awk-F:'{printf "|%-15s |%-10s |%-15s |\ n", $1meme / etc/passwd

% s character type

D numeric type

Account for 15 characters

-indicates left alignment. Default is right alignment.

Printf does not automatically wrap lines at the end of a line by default, adding\ n

= =

VI. Awk mode

1. Regular expression:

Match record (whole row):

# awk'/ ^ alice/' / etc/passwd# awk'$0 ~ / ^ alice/' / etc/passwd# awk'! / alice/' passwd# awk'$0! ~ / ^ alice/' / etc/passwd

Match field: match operator (~! ~)

# awk-F:'$1 ~ / ^ alice/' / etc/passwd# awk-F:'$NF! ~ / bash$/' / etc/passwd

2. Comparison expression:

The comparison expression compares the text and performs the specified action only if the condition is true. Comparison expressions use relational operators

Used to compare numbers with strings.

Relational operator

Example of operator meaning

< 小于 x 大于 x>

Y # awk-F:'$3 = = 0' / etc/passwd# awk-F:'$3

< 10' /etc/passwd# awk -F: '$7 == "/bin/bash"' /etc/passwd# awk -F: '$1 == "alice"' /etc/passwd# awk -F: '$1 ~ /alice/ ' /etc/passwd# awk -F: '$1 !~ /alice/ ' /etc/passwd# df -P | grep '/' |awk '$4 >

25000'

3. Conditional expression:

# awk-F:'$3 > 300 {print $0}'/ etc/passwd# awk-F:'{if ($3 > 300) print $0}'/ etc/passwd# awk-F:'{if ($3 > 300) {print $0}}'/ etc/passwd# awk-F:'{if ($3 > 300) {print $3} else {print $1}'/ etc/passwd

4. Arithmetic operation: +-* /% (module) ^ (power 2 ^ 3)

Calculations can be performed in mode, and awk will perform arithmetic operations as floating-point numbers

# awk-F:'$3 million 10 > 500'/ etc/passwd# awk-F:'{if ($3 million 10 > 500) {print $0}}'/ etc/passwd

5. Logical operators and composite mode

& & Logic and astatb | | Logic or a | | b! Logic is not! a # awk-F:'$1~/root/ & & $3

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