(Viii) awk command

A: printf formatted output

% Ns: output string. n is a number to refer to output several characters

% Ni: integer output. n is a number to refer to the digital output of several

% M.nf: floating-point output. m and n are numbers, integer digits and decimal places of generations output. As% 8.2f

Representative common output 8 bits, two bits are decimal, 6 is an integer.

split , substr , strlen

Output formats:

\ A: output warning sound

\ B: output backspace key, which is the Backspace key

\ F: Clear Screen

\ N: line feed

\ R: Enter, Enter key is

\ T: horizontal output backspace key, which is the Tab key

\ V: vertical output backspace key, the Tab key is

printf '%s' $(cat student.txt)

printf '%s\t %s\t %s\t %s\t %s\t %s\t \n' $(cat student.txt)

3: awk basic use

awk '{action 1 Condition 1 Condition 2} {2} ... operation' file name

Conditions (Pattern):

Relational expression is generally used as a condition. The relational expression is very large, particularly with reference to Table 12-3, for example:

x> 10 judges whether the variable x is greater than 10

x == y is determined whether the variable x is equal to the variable y

A ~ B determines whether a string contains the substring A matches B expression

A! A ~ B determines whether the string does not contain substring matches B expression

 

(1) Output Formatting awk

awk '{printf $1 "\n"}' student.txt

printf '%s' $(cat student.txt)

awk '{printf $2 "\t" $6 "\n"}' student.txt

# Output of the second row and the sixth column

 

 

 

(2) conditions printing output fraction "of 90 names of classmates

grep -v "Name" student.txt | awk '$4>=90{ printf $2 "\n" }'

 

(3) awk execution

1: If there BEGIN condition, the first performing an action defined BEGIN

2: BEGIN If no condition is read in a first row, the first row of data sequentially assigned to $ 0, $ 1, $ 2 variables. $ 0 wherein data representative of the entire trip, $ 1 represents the first field, $ 2 represents the second field.

3: type judgment based on the condition whether the operation performed. If the condition is met, perform an action, or read the next line of data. If there are no conditions, each row to perform an action.

1 reads the next row of data, repeating the above steps.

 

4: awk built-in variable

$ 1 represents the first column, etc.

$ 0 represents the entire row of data awk currently being read. We know awk is read into the data line by line, $ 0 represents the entire row of data is read into the current line.

$ N represents the n-th field currently read into the line.

Field owned (column) NF total current line.

NR awk currently processed row, the first few lines of the total data.

FS user-defined delimiters. awk's default delimiter is any space. If you want to use other delimiters (such as ":"), you need to define the variable FS.

Examples: The FS user defined view delimiter

cat /etc/passwd | grep  "/bin/bash" | awk 'BEGIN {FS=":"} {printf $1 "\t\n" }  '

Guess you like

Origin www.cnblogs.com/love-life-insist/p/11668790.html