Linuxday18--awk

Source: Internet
Author: User
Tags rand

awk Introduction

Awk:aho, Weinberger, Kernighan, Report generator, formatted text output available in multiple versions: New awk (Nawk), GNU awk (gawk)

Gawk: Mode scan and processing language

Basic usage:

awk [Options] ' program ' var=value file ...

awk [Options]-F programfile var=value file ...

awk [Options] ' begin{action; ...} pattern{action; ...} end{action; ...} ' File ...

The awk program usually consists of a BEGIN statement block, a universal statement block capable of using pattern matching, and an end statement block, consisting of a total of 3 parts

Program is usually enclosed in quotation marks or double quotes

Options:

-F indicates the field delimiter to use when entering

-V var=value: Custom variable

awk language

Basic format: awk [options] ' program ' File ...

program:pattern{action statements;..}

Pattern and action:

The pattern section determines when an action statement triggers and triggers an event

  Begin,end

Action statements the data and places it within {} to indicate

  Print, printf

separators, fields, and records

When Awk executes, a delimiter-delimited field (field) tag $1,$2: $n is called a domain identity. $ $ $ For all domains, note: and Shell variable $ characters have different meanings

Each line of the file is called a record

Omit action, default to print $

How awk Works

First step: Execute Begin (table header) {action; ...} Statements in a statement block

Step two: Read a line from the file or standard input (stdin), then execute the pattern{action ...} Statement block, which scans the file row by line, repeating the process from the first line to the last line until the file is fully read.

Step three: When reading to the end of the input stream, execute end (rollup) {action; ...} Statement block

The BEGIN statement block is executed before awk begins to read rows from the input stream, which is an optional block of statements, such as variable initialization, table-top statements for printed output tables, which can usually be written in the BEGIN statement block

The end statement block is executed after awk reads all the rows from the input stream, such as the analysis results for all rows, such as a summary of information that is done in the end statement block, which is also an optional statement block

The General command in the pattern statement block is the most important part and is optional. If the pattern statement block is not provided, the default is {print}, which prints every fetched row, and every row that awk reads executes the statement block

awk variable

Variables: Built-in and custom variables

FS: Enter field delimiter, default to white space character

Awk-v fs= ': ' {print $1,fs,$3} '/etc/passwd

Awk–f: ' {print $1,$3,$7} '/etc/passwd

OFS: Output field delimiter, default to white space character

Awk-v fs= ': '-v ofs= ': ' {print $1,$3,$7} '/etc/passwd

RS: Enter a record delimiter to specify the line break at input

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

ORS: Output record delimiter, output with specified symbol instead of line break

Awk-v rs= "-v ors= ' # # # ' {print} '/etc/passwd

NF: Number of fields

Awk-f: ' {print NF} '/etc/fstab, reference built-in variable without $

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

NR: Record number

awk ' {print NR} '/etc/fstab; awk END ' {print NR} '/etc/fstab

FNR: Each file counts separately, record number

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

FileName: Current file name

awk ' {print FILENAME} '/etc/fstab

ARGC: Number of command line arguments

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

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

ARGV: An array that holds the arguments given by the command line

awk ' BEGIN {print argv[0]} '/etc/fstab/etc/inittab

awk ' BEGIN {print argv[1]} '/etc/fstab/etc/inittab

Custom variables (character case sensitivity)

(1)-V Var=value

(2) directly defined in program

Example:

Awk-v test= ' Hello gawk ' {print test} '/etc/fstab

Awk-v test= ' Hello gawk ' begin{print test} '

awk ' begin{test= ' Hello,gawk ";p rint test} '

Awk–f: ' {sex= ' male ';p rint $1,sex,age;age=18} '/etc/passwd

Cat Awkscript

{Print script,$1,$2}

Awk-f:-F awkscript script= "awk"/etc/passwd

printf Command

Formatted output: printf "format", Item1, item2, ...

(1) format must be specified

(2) does not wrap automatically, you need to explicitly give the line-break control, \ n

(3) format must be specified for each subsequent item.

Format character: Corresponds to item one by one

%c: ASCII code for displaying characters

%d,%i: Displays decimal integers

%e,%e: Display scientific counting method values

%f: Displayed as a floating-point number

%g,%g: Displaying values in scientific notation or floating-point form

%s: Display string

%u: unsigned integer

Percent: show% itself

Modifier:

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

-: Left-justified (default right-aligned)%-15s

+: Displays the positive and negative sign of the value%+d

Awk-f: ' {printf '%-20s:%5d\n ', $1,$3} '/etc/passwd

awk Control Statements

If-else

Syntax if (condition) {statemrnt; ...} [Else statement]

if (condition) {statrment1}else if (condition2) {Statement2}

ELSE{STATEMENT3}

Usage Scenario: for awk

While loop

Syntax: while (condition) {statement; ...}

Condition "true", enter loop; condition "false", exit loop

Usage scenarios:

Pay attention to multiple fields in a row using similar processing

Each element in an array is processed using the

Do-while Cycle

Syntax: Do{statemrnt, ...} while (condition)

Meaning: Perform at least one cycle, whether true or false

For loop

Syntax: for (EXPR1;EXPR2;EXPR3) {statement; ...}

Switch statement

Syntax: switch (expression) {case VALUE1 or/regexp/: statement1, Case VALUE2 or/regexp2/: statement2; ...; default:statementn }

Break and Continue

awk Array

Associative array: array[index-expression]

Index-expression:

(1) You can use any string; strings are enclosed in double quotation marks.

(2) If an array element does not exist beforehand, when referenced, awk automatically creates this element and initializes its value to "empty string"

To determine if an element exists in an array, use the "index in array" format to traverse

To iterate through each element in the array, use the For loop

for (var in array) {For-body}

Note: Var iterates through each index of the array

awk function

Numerical Processing:

RAND (): Returns a random number between 0 and 1

awk ' Begin{srand (); for (i=1;i<=10;i++) print int (rand () *100)} '

String processing:

Length ([s]): Returns the length of the specified string

Sub (r,s,[t]): searches the T string for the pattern match of the R representation and replaces the first match with the S

echo "2008:08:08 08:08:08" | awk ' Sub (/:/, "-", $) '

Gsub (R,s,[t]): searches the T string for the pattern-matching contents of the R representation, and replaces all of the contents represented by S

echo "2008:08:08 08:08:08" | awk ' Gsub (/:/, "-", $) '

Split (S,array,[r]): Takes R as a delimiter, cuts the string s, and saves the resulting cut to the array represented by array,

The first index value is 1 and the second index value is 2,...

Calling the shell command in awk

System command

The space is a string connector in awk, and if you need to use a variable in awk in system, you can use the

Space-delimited, or other than awk variables, are all referenced with "".

Linuxday18--awk

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.