Simple examples of Text Analysis Tool awk and text awk
Create a file: vim hi
Take 2nd fields and 3rd fields:
Awk '{print $2, $3}' hi note {}, CommaWill be converted to a space in the output.
Character Description:
Display the entire line:
Field separator:-F
Built-in variable: NF: number of fields. $ NF indicates the last field.
NR: number of rows
/Regular expression/: only the rows that can be matched by the pattern here are processed.
Backend: awk '! /^ John/{print $2} 'hi
The preceding statement indicates that if the third field is greater than or equal to 500, the first and third fields are used.
Determine whether the last field is/bin/bash. If yes, print the first and last fields.
Perform pattern matching
Cat hi
Match row range
Get row range:
If condition judgment statement: determines whether the value of the third field is greater than 500
Character replacement:
Echo "this is a test" | awk 'gsub ("test", "abd ")'
Cat hi
While loop:
NF indicates the number of fields in the current row. length is a function that obtains the character length of $ I.
Awk '/^ tom/{I = 1; while (I <= NF) {if (length ($ I)> = 4) {print $ I, length ($ I)}; I ++} 'hi
The if statement is nested in the while loop.
The preceding while statement can also be replaced by a for loop statement.
Awk '/tom/{for (I = 1; I <= NF; I ++) {print $ I, length ($ I)}' hi