awk command Guide

A: awk implementation process

(1) First, the initialization operation performed BEGIN {} block
(2) and then recycled to the designated data file is read in a row of data (automatically updated NF, NR, $ 0, $ 1 ....., and the like of the built-in variables value)
(3) performs the last subsequent processing operations of block END {}

Two: awk options
-F ":" delimiter specify each field
-v var = value Custom Variables

Three: the basic use awk

  1. Output command

print item1,item2

Key: (1), as the number of separator
(2) the output of each item can be a string, may be a numeric value, an expression of the current record fields, or variables of awk
Example: head -4 / etc / fstab | awk '{Print "Hello", $}. 1'
(. 3) is omitted if the item. Equivalent to $ 0, print the entire line

2. Variables

(1) built-in variables

FS: Field Separator input equal to -F option defaults blank
OFS: when the output field separator, default blank
RS: When the input line separator, default \ n-
the ORS: when the output line separator, default \ n Example: awk -F ":" -v the OFS = ":" '{Print $. 1, $ 2, $. 3}' / etc / the passwd
of NF: number of fields, the line currently processed (columns)
Note: $ NF Print last a field
NR: number of rows currently processed
the FNR: each of the files are counted (number of rows)
FILENEME: awk browse file name
$ 0: currently processed entire line row
$ n: the currently processed n-th field (n-th column )
ARGC: Example number command line parameters: awk 'Print ARGC the BEGIN {}' / etc / fstab / etc / issuee
ARGV: array, is stored in the command line parameters given

awk execution process:
(1) First, initialization is performed BEGIN {} block are:
(2) Then a loop reads data from the specified row in the data file automatically updates {NF, NR, $ 0, $ 1 ... .} value or the like of built-in variable)
and performs 'edit mode or conditional instruction {}':
. (3) Finally, perform the END {} block of the subsequent processing operations
awk options

-F ":": Specify each field separator, default spaces
-v var = value Custom Variables

1. Output command
print item1, item2

Key: (1) a comma separator
of each item (2) may be output string may be numeric, an expression of the current record fields, variables, or the awk
head -5 / etc / fstab | awk '{print "hello , "$ 1} '
(3) Item If omitted, the equivalent of $ 0, printing the entire line
2, variable

(1) built-in variable
FS: Field Separator input equal to -F default blank
OFS: when the output field separator default blank
RS: When the input line separator, default \ n-
the ORS: line separator at the output, default is \ n-
awk -F ":" -v the OFS = ":" '{Print $. 1, $ 2, $. 3}' / etc / the passwd
number of fields NF current row processed (columns)
Note: $ NF: Print the last field
NR currently processed order number (number of rows) row of
the FNR: each file separately count: number of rows
fILENAME awk browse file name
entire line row $ 0 currently processed
$ n currently processed n-th field (n column)
ARGC: the number of command-line arguments
awk 'Print ARGC the BEGIN {}' / etc / fstab / etc / Issue
. 3
ARGV: array, is stored in the command line parameters given:
awk 'the BEGIN {Print ARGV [0]} '/ etc / fstab / etc / Issue
awk
awk' the BEGIN {Print ARGV [. 1]} '/ etc / fstab / etc / Issue
/ etc / fstab
awk' the BEGIN {Print ARGV [2]} '/ etc / fstab / etc / Issue
/ etc / Issue

(2) Custom Variables
defined method

  1. -v var=value
  2. BEGIN {var = value}

(3) awk variable using linux

3.printf
formatted output: printf format, item1, item2, ...

(1) format must give
(2) does not wrap, given the need for explicit newline delimiter, \ n------
(. 3) are required to specify the format of each item a formatted symbol behind;

Specifier
% c: show the ASCII
% d,% i display decimal integer
% e,% E numerical display scientific notation
% f shown as floating
% g,% G displayed in scientific notation or a floating-point format values
% display string s
% u unsigned integer
%% display itself%

例子
awk -F ":" '{printf "Username: %s\n", $1}' /etc/passwd

awk -F: '{printf "Username: %s, UID: %d\n",$1,$3}' /etc/passwd

Modifiers

[. #]: The first digit is used to control the width of the display too, the second decimal point accuracy.

% 3.1f
Examples
awk -F: '{printf "Username :% 10s, UID:% d \ n", $ 1, $ 3}' / etc / passwd
default right alignment

-: Left
awk -F: '{printf "Username :% -10s, UID:% d \ n", $ 1, $ 3}' / etc / passwd

+: Display symbols values
awk -F: '{printf "Username :% -10s, UID:% + d \ n", $ 1, $ 3}' / etc / passwd

4. Operators
(1) arithmetic operators
x + y, xy, x * y, x / 5, x ^ y, x% y

+ X: conversion to numbers

(2) assignment operator
+ = = - = = * /% = = = ^
+ -

(3) comparison operators:
<Less than
<= Less than or equal to
== Equal
= Not equal to!

= Greater than or equal
~ matches the regular expression
! ~ Does not match the regular expression

(4) matching at pattern
- matching! ~ Mismatch

(5) logical operators
&& ||!

(6) function call
FUNCTION_NAME (parameter 1, parameter 2)

(7)条件表达式
selector ? if-true-expression;if-false-expression
awk -F: '{$3>=500?usertype="Common User":usertype="Sysadmin or SysUser";printf "%15s:%-s\n",$1,usertype}' /etc/passwd

  1. The PATTERN (mode)
    (. 1) empty:. Air mode, matching each row
    (2) ./ regular expression /: here only be matched to the pattern line
    awk '/ ^ UUID / {print $ 1}' / etc / fstab

(3) releational expression relational expression, there are really false results, will be processed as true
awk -F: '$. 3> = 500. 1 {Print $, $. 3}' / etc / the passwd
awk -F: '$ of NF == " / bin / the bash "$ {Print. 1, of NF} $ '/ etc / the passwd
awk -F:' $ ~ of NF / the bash $ / $ {Print. 1, of NF} $ '/ etc / the passwd
4.line Ranges: row range
specified from start line, end line
cat / etc / passwd | awk -F : '/ ^ root /, / ^ sync / {print $ 1}'
true: the result is non-zero value, non-empty string is true

6.BEGIN / END pattern
before editing instruction BEGIN {} // Start first line of text processing operations
after the operation of the editing instruction END {} // after last line of text

Built-in functions

length () Gets the string length

Exercises
practice text
CAT grade.txt
M.Tansley. 8 40 05/99 48311 Green 44 is
J.Lulu Green. 9 06/99 48317 24 26 is
P.Bunny Yellow 12 is 35 48 02/99 26 is
J.Troll 48 07/99 42 is Brown 12 is 26 is 26 is -3
L.Tansley 05/99 47 30 12 is 28 12 is 2-Brown

This file has seven fields

Printing paper
awk '{print}' grade.txt

Printing the first column and the seventh column
awk '{print $ 1, $ 7}' grade.txt

Printing line 2-4 rows
awk '(NR> = 2) && (NR <= 4) {print}' grade.txt

Print the contents of the first row and the third row

awk 'NR==1||NR==3' grade.txt

Added at the beginning of the file header
awk 'BEGIN {print "date age \ n -----"} {print}' grade.txt

In the end of the additional content file
awk '{print} END {print "BY linux"}' grade.txt

Output to the screen output to a file while
awk '{print} END {print "BY linux"}' grade.txt | tee a.txt

Analyzing entire row matches Brown, the entire match line output

awk '$0~/Brown/ {print $0}' grade.txt

Analyzing the third column is equal to 48, the entire match line output

awk '$3=="48" {print}' grade.txt

Analyzing the entire row do not match Brown, output does not match the whole line
awk '$ 0! ~ / Brown / {print $ 0}' grade.txt

Analyzing the fourth column is equal to Brown-2, the whole line is not equal to the output

awk '$4!= "Brown-2" {print $0}' grade.txt

Green filter file or green

awk '/[Gg]reen/ {print $0}' grade.txt

Analyzing an arbitrary three letters at the beginning of a front
awk '/ [Gg] reen / {print $ 0}' grade.txt

Analyzing the entire row or matching Yellow Brown
awk '$ 0 ~ / (Yellow | Brown) / {0} Print $' grade.txt

P at the beginning of the line filter
awk '$ 0 ~ / Brown / {print $ 0}' grade.txt

Statistical rows and columns
awk 'END {print NR}' grade.txt

awk 'END{print NF}' grade.txt

Printing / etc / first column and the seventh column passwd file
awk -F ":" '{Print $. 1, $. 7}' / etc / passwd
awk 'the BEGIN {the FS = ":"} {Print $. 1, $. 7}' / etc / passwd

All odd outputs
awk '(NR% 2) == 1 {print}' grade.txt

All even output
awk '(NR% 2) == 0 {print}' grade.txt

Statistics for the current user name for all system users, UID, GID, login shell, made of EXCEL spreadsheet windows system
$ 7 $ NF can be used in place of (the last one)

awk -F ":" '{print $1","$3","$4","$7}' /etc/passwd > user.csv

The penultimate column of print content
awk -F ":" '{print $ (NF-1)}' / etc / passwd

When the memory is used more than 85% of the alarm (rounded)
[$ (Free -m | awk '/ Cache: / {Print int ($. 3 / ($. 3 + $. 4) * 100)}') -gt 85] && echo "Memory It has been used more than 85%. "

awk shell variables referenced

name="zhangsan"

awk -v nm="$name" 'BEGIN{print nm}'

awk -v nm="abc" 'BEGIN{print nm}'

awk conditional statement

A single limb format
'{if (expression) Command 1; 2 command;} ....'

例子
awk '{if($6<$7)print $7 too high}' grade.txt
awk '{if($4~/Brown/) print $0}' grade.txt
awk '{if($1=="P.Bunny"&&$4=="Yellow ")pinrt $0}' grade.txt

awk '{if($1=="P.Bunny"||$4=="Yellow ")pinrt $0}' grade.txt

awk '{name=$1;belets=$4;if(belets~/Yellow/)print name}' grade.txt

awk 'BEGIN{basefile="27"}{if($6<basefile)print $1}' grade.txt

if / else else if statement determines multiple
'{if (expression) Command 1; Command 2; ...;
the else IF (expression) {command 1; 2 command; ..; 1 the else command, the command 2; .. .} '

例子
awk '{if($7==44)print $1;else if($7==28)print $2;else print $3}' grade.txt
awk '{if($7==44)print $2;else print $1}' grade.txt

cycle

do while loop for loop loop

while statement

while (expression) {statement}

example

The initial value of the variable 1, if i is less than equal to NF (the number of fields in the record), the print statement is executed, cut i increments by 1 until i> NF

awk 'NR==1{i=1;while(i<=NF){print NF,$i;i++}}' grade.txt

awk 'BEGIN{test=100;total=0;while(i<=test){total+=i;i++;}print total;}'

for loop

Format 1

Convenient array elements
for (variable in array) {statement}
awk 'the BEGIN {for (in the ENVIRON K) {Print K "=" the ENVIRON [K];}}'

Format 2

for (variable; condition; expression) {statement}
awk 'NR. 1 == {for (I =. 1; I <= of NF; I ++) Print of NF, $ I;}' grade.txt

awk 'BEGIN{total=0;for(i=0;i<=100;i++){total+=i;} print total;}'

break when the break statement is used while or for statement causes an exit program loop

When the continue statement continue for whiler or for statement so that the program moves to the next loop iteration

It can lead to next read next input row, and returns to the top of the script. This avoids input line execution of other operations on the current

exit statement and exit the main loop control to END; END if present; withdraw does not exist

do-while loop (regardless of whether the first condition is true, are the first time through the loop)

format:

do

{Statement} while (condition)

awk 'BEGIN{ total=0; i=0; do { total+=i; i++; }while(i<=100) print total; }'

Array

Associative array array [element array]
(1) may be used any string, string to double quotation marks
(2) If an array element does not exist beforehand, in reference, awk automatically creates this element, and initializes its value "empty string"

To determine whether there is an element of the array, to be carried out using the "index in array" format

wekdays=[mon]="monday"

awk 'BEGIN{wekdays["mon"]="Monday";wekdays["Tue"]="Tuesday";for (i in wekdays) {print wekdays[i]}}'

i will go through each variable array index wekdays

Acquisition system tcp connection status statistical
netstat -tan | awk '/ ^ tcp > / {state [$ NF] ++} END {for (i in state) {print i, state [i]}}' | sort - rn

Note: The actual work netstat efficiency is relatively low, it is recommended to use the command ss

ss -antup | awk '/tcp>/{state[$2]++}END{for (i in state){print i,state[i]}}'| sort -rn

ss -antup | awk '/udp>/{state[$2]++}END{for (i in state){print i,state[i]}}'| sort -rn

IP access statistics specific number of
cat / var / log / httpd / access_log | awk '{ip [$ 1] ++} END {for (i in ip) {print i; ip [i]}}'

Functions
1. The built-in function
rand (): returns a random number between 0 and 1
with a awk command values are taken to the same

String processing
lengeth ([s]) Returns the length of the string
sub (r, s, [t ]): r represents the mode looking for a match to the string represented by t. Matched content and replacement of content s expressed
slice
split (s, a [, r ]): In a cutting string delimiter r is s, and the results are stored in the array to a cut indicated by
an array of awk index numbered starting from 1

Guess you like

Origin www.cnblogs.com/bingguoguo/p/10994649.html