awk Simple Application explanation

Source: Internet
Author: User
Tags arithmetic operators

Gawk:pattern scanning and processing language

Syntax: gawk [options] ' program ' FILE ...

Program:pattren{action statements}

Options:

-F: Indicates the field delimiter when entering data

-V var=value: Custom variable

1. Print

Print item1,item2, ...

Key points: (1) Comma delimiter

(2) Each item of the output can be a string, a value, a field (using & Reference), a variable (directly input a variable), or an awk expression

(3) If item is omitted, it is equivalent to print $


2. Variables

built-in variables

fs:input field seperator input delimiter: default to white space character

Ofs:onput field seperator output delimiter: Default to white space character

Rs:input record seperator: line break at input

Ors:output record seperator: line break at output

Nf:number of field: Number of fields per row

Nr:number of Recor: Rows

FNR: Each file counts: number of rows

FileName: Current file name

ARGC: Number of command-line arguments (use begin to show only once before the command)

ARGV: Array argv[#]: Saves the parameters given in the command line

Custom variables

(1)-V var=value case-sensitive

(2) directly defined in program

awk ' {test= ' 1 ";p rint test} ' filename

If the file is not processed: awk ' begin{test= ' 1 ";p rint test} '

3. printf command

Formatted output: printf format (format character), ITEM1,ITEM2, ...

(1) FORMAT must give the

(2) does not wrap automatically, need to display the line feed control \ n

(3) format needs to specify a formatting symbol for each subsequent item, respectively

Format characters:

%c: ASCII code for displaying characters

%d,%i: Displaying decimal integers

%e,%e: Numerical display of scientific counting method

%f: Floating point

%g,%g: Scientific counting method or floating point number

%s: string

%u: unsigned integer

Percent: show% itself

Modifier:

#[.#]: The width of the first digital control display, the second shows the precision after the decimal point (%3.1f)

-: Left-justified, default right-justified

+: Displays the value of the symbol (plus or minus)

4. Operator

arithmetic operators

Binocular: +-*/^%

Monocular:-X +x

String operator: unsigned operator, string connection

Assignment operator: = + = = *= ...

Comparison operators: > >= < <= = = =

Pattern match: ~ match

! ~ No match

Logical operator:&& | | !

Function call: function_name (ARGU1,ARGU2,...)

Conditional expression: select?if-true-expression:if-false-expression

5. Pattern

(1) Empty: null mode, matching each row

(2)/regular expression/: Regular expression, processing only matching rows

(3) conditional expression/relationship expression: The result is "true" "false" and the row with the result is "true"

True: The result is a non-0 value, non-empty string

(4) line ranges: Range

STARTLINE,ENDLINE:/PAT1/,/PAT2/indicates a range by pattern matching

Formats that give numbers directly are not supported

Awk-f: ' (nr>=2&&nr<=10) {print '} '/etc/passwd

(5) Begin/end mode

begin{}: A program that executes only once before beginning the processing of text in a file

end{}: Only once after text processing is complete

6. Common action

(1) expressions

(2) control statements:if,while, etc.

(3) Compound statements: Combined statement:

(4) Input statements

(5) Output statements

7. Control statements

if (condition) {statements} else {statements}

while (conditon) {statements}

do {statements} while (Conditon)

for (expr1;2;3) {statements}

Break

Continue

Delete Array[index]

Delete Array

exit

{Statements}

Example:

1,-F: Indicates the field delimiter when entering data, and defaults to white space characters. The example uses ":" as a delimiter and outputs the first set of elements:

[Email protected] ~]# Tail-n 5/etc/passwd | Gawk ' {print $} ' Pulse:x:497:496:pulseaudiosshd:x:74:74:privilege-separatedtcpdump:x:72:72::/:/sbin/nologinarmo: X:500:500:centosnamed:x:25:25:named:/var/named:/sbin/nologin[[email protected] ~]# tail-n 5/etc/passwd | Gawk-f: ' {print '} ' pulsesshdtcpdumparmonamed

2,-v Var=value and begin use: Separate begin does not process text, only output actions, combined with the use of custom variables: (You can use the option definition, but also directly in the program defined variables)

[[email protected] ~]# awk-v Test=armo ' begin{print test} ' armo[[email protected] ~]# awk ' begin{test= ' Armo ';p rint test} ' Armo

3, the use of built-in variables: NR: Display line number

[Email protected] ~]# Tail-n 5/etc/passwd | Awk-f: ' {print nr,$1,$2} ' 1 pulse x2 sshd x3 tcpdump x4 Armo x5 named X[[email protected] ~]#

4. Formatted output of printf

[[email protected] ~]# awk -f:  ' {printf  ("%s,%s\n", $1,$2)} '  /etc/passwdroot , xbin,xdaemon,xadm,xlp,x ... [[email protected] ~]# awk -f:  ' {printf  ("%15s    --%5s\n", $ 1,$2)} '  /etc/passwd           root     --    x             bin    --    x          daemon    --    x             adm    --    x              lp    --    x            sync    --     x       shutdown    --    x            halt    --     x           mail    --     x First domain character width 15, second field character Wideband 5[[email protected] ~]# awk -f:  ' {printf   ("%-15s%+5.3f\n", $1,$3)} '  /etc/passwdroot            +0.000bin            +1.000daemon          +2.000adm             +3.000lp              +4.000sync           +5.000shutdown        +6.000 first field left-aligned width 15 output, third field displays numeric positive and negative, width 5, decimal point accurate to three-bit output 

4. Examples of operator usage instructions

[[email protected] ~]# awk-v a=2-v b=1 ' Begin{print "sum=" a+b} ' sum=3[[email protected] ~]# awk-f:-v a=2-v b=1 ' {$3&G T;=5? Sum=a+b:sum=a+a;print $1,sum} '/etc/passwdroot 4bin 4daemon 4adm 4lp 4sync 3shutdown 3halt 3mail 3UUCP 3

5. Pattern Example

[Email protected] ~]# awk-f: ' (nr>=2&&nr<=10) {print nr,$1} '/etc/passwd2 bin3 daemon4 adm5 lp6 sync7 Shutd OWN8 halt9 mail10 uucp[[email protected] ~]#


awk Simple Application explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.