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

Detailed explanation and examples of awk

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

AWK language

V option:

-F: indicates the field delimiter used when entering

-v var=value: define variables

V delimiters, fields, and records

When awk is executed, the field (field) separated by a delimiter is marked $1 ~ 2.

Identifies the domain. $0 is all fields. Note: the meaning of the variable $symbol in shell is different.

Each line of the file is called a record

If the action line is omitted, the print $0

$0: print all

V example:

Awk'{print "hello,awk"}'

Awk-F:'{print}'/ etc/passwd

Awk-F:'{print "wang"}'/ etc/passwd

Awk-F:'{print $1}'/ etc/passwd

Awk-F:'{print $0}'/ etc/passwd

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

Tail-3 / etc/fstab | awk'{print $2 recording 4}'

Awk variable

V variables: built-in and custom variabl

V FS: enter the field delimiter, which defaults to a blank character

Awk-v FS=':''{print $1m FS journal 3}'/ etc/passwd

Awk-F:'{print $1, 3, 7}'/ etc/passwd

V OFS: output field delimiter, default is white space character

Awk-v FS=':'-v OFS=':''{print $1 recording 3 memes 7}'/ etc/passwd

VRS: enter the record delimiter and specify the newline character when entering. The original newline character is still valid.

Awk-v RS='''{print}'/ etc/passwd

V ORS: output record delimiter, using the specified symbol instead of the newline character

Awk-v RS=''- v ORS='###'' {print}'/ etc/passwd

Print odd or even lines:

Some simple methods of calculation

Count the number of words in a file:

NF: number of field

Awk-FRV:'{print NF}'/ etc/fstab, no $is needed to refer to the built-in variable

Awk-F:'{print $(NF-1)}'/ etc/passwd

V NR: line number

FNR: count each file separately, line number

Awk'{print FNR}'/ etc/fstab / etc/inittab

FILENAME: current file name

Awk'{print FILENAME}'/ etc/fstab

Print out the file name when you print it out.

ARGC: the number of command line arguments

Awk'{print ARGC}'/ etc/fstab / etc/inittab

Awk 'BEGIN {print ARGC}' / etc/fstab / etc/inittab

V ARGV: array that holds the parameters given by the command line

Custom variables (case sensitive)

(1)-v var=value

(2) in program

The difference between putting variables in {} and outside:

{} inside: assign a value each time it is executed

{} outside: assign values before execution and recycle later

-f: invoke the script

Printf command

There are several variables behind, and there should be several format definitions in front of them, that is, several% definitions, percent sign definitions and variables should correspond one to one.

V formatted output: printf "FORMAT", item1, item2,...

(1) FORMAT must be specified

(2) the line wrapping will not occur automatically, and the newline control character needs to be given explicitly,\ n

(3) format characters need to be specified for each subsequent item in FORMAT

V format character: one-to-one correspondence with item

% c: ASCII code for display characters

% d,% I: show decimal integers

% e,% E: display scientific counting values

% f: displayed as a floating point number

10.3: a total of 10 digits, including 3 zeros, one decimal point

% g,% G: display values in scientific counting or floating point form

% s: display string

% u: unsigned integer

The following figure shows 10 characters:

%%: show% itself

V modifier:

# [. #]: the first number controls the width of the display; the second # indicates the precision after the decimal point,% 3.1f

-: left alignment (default right alignment)%-15s

+ sign: displays the plus or minus sign% + d of the numeric value

BEGIN: add a header in front of it

Operator

V arithmetic operator:

X% y, x ^ y, x% y

-x: convert to negative number

+ x: convert to numeric value

V string operator: unsigned operator, string concatenation

V assignment operator:

=, +, -, *, /,%, ^ =

+,-

V comparison operator:

=,!, >, >, v2?var=v1:var=v2

Return var

}

BEGIN {axiom 3 / b / b / 2 / print max (b)}

Awk script

V will write the awk program as a script, which can be called or executed directly

V example:

# cat f1.awk

{if ($3 > = 1000) print $1

# awk-F:-f f1.awk / etc/passwd

# cat f2.awk

#! / bin/awk-f

# this is an awk script

{if ($3 > = 1000) print $1

# chmod + x f2.awk

# f2.awk-F: / etc/passwd

Pass parameters to the awk script

V format:

Awkfile var=value var2=value2... Inputfile

V Note: not available during BEGIN. Until the first line of input is complete, change

Quantity is only available. You can use the-v parameter to let awk get it before executing BEGIN

The value of the variable Each specified variable on the command line requires a-v argument

V example:

# cat test.awk

#! / bin/awk-f

{if ($3 > = min & & $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

Database

Wechat

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

12
Report