Tag: Use the number family font else to manipulate the line-wrap output range
I. AWK syntax
awk [options] ' commands ' files
Option
-F defines the field delimiter, and the default delimiter is a contiguous space or tab
Define the interval symbol using the-f parameter in option
Use the order of $1,$2,$3 to represent the different fields of each row in files separated by the interval symbol
NF variable indicates the number of fields in the current record
-V Define variables and assign values can also be borrowed from the shell variable to introduce
Two. Variables
Variable naming rules
Start with a letter or underscore, and the rest can be: letters, numbers, underscores.
It is advisable to follow the following specifications:
1. Start with a letter
2. Connect the words with an underscore or an underscore
3. Differentiation of the same type with numbers
4. The best name for the file plus extension
NR indicates the number of rows that awk reads in
FNR indicates the number of rows in the file where the entry is being read
# awk ' {print nr,fnr,$1} ' file1 file2
1 1 aaaaa
2 2 bbbbb
3 3 CCCCC
4 1 dddddd
5 2 eeeeee
6 3 ffffff
#
logical operations can directly refer to fields for Operation
= = >= <=! = > < ~!~
# awk ' nr==1 {print} '/etc/passwd
Root:x:0:0:root:/root:/bin/bash
#
Command {print $}
read after processing END {awk_cmd1;awk_cmd2;}
NR Current Record count (all files after connection statistics)
FNR Number of current records (only statistics for the current file, not all)
the FS field separates defaults as consecutive spaces or tabs, you can use several different symbols to do the delimiter-f[:/]
OFS The delimiter of the output character is a space by default
# awk-f: ' ofs= ' ===== ' {print $1,$2} '/etc/passwd
root=====x
NF number of fields currently read
ORS output record delimiter default is line wrapping
# awk-f: ' ors= ' ===== ' {print $1,$2} '/etc/passwd
Root X=====bin x=====
FileName Current file name
Methods for referencing shell variables
# A=root
# awk-v Var= $a-f: ' = = var {print $} '/etc/passwd
Or take the entire command apart and let the shell variable be exposed,
# awk-f: ' $ = = ' $a ' "{print $} '/etc/passwd
# A=NF
# awk-f: ' {print $ ' $a '} '/etc/passwd
three. Operators
Assign Value
= += -= /= *=
Logical and logical or logical non-
&& | |!
match Regular or mismatched, regular need/regular/surround
~ !~
strings are enclosed in double quotation marks when comparing strings.
< <= > >= = = =
Field Reference
the $ field reference needs to be added, and the variable reference is taken directly with the variable name
operator
+ - * / % ++ --
Escape Sequences
\ \ Self
\$ Escape $
\ t Tab
\b Backspace
\ r return character
\ n line break
\c Cancel line break
Four.if (condition) Action
If there are multiple actions, enclose the action body in curly braces if (condition) {action 1; Action 2}
# awk-f: ' {if ($ = = "Root") print $ '/etc/passwd
Root
#
# awk-f: ' {if ($ = = "Root") {print $1;print $6}} '/etc/passwd
Root
/root
#
if (condition 1)
Action 1
Else
Action 2
# awk-f: ' {if ($ = = "Root") {print $1}else print $6} '/etc/passwd
# awk-f: ' {if ($ = = "root") print $1;else print $6} '/etc/passwd
The above two commands are equivalent, either separated by semicolons, to indicate the end of the first action body, or to position the action body with braces
if (condition 1)
Action 1
else if (condition 2)
Action 2
else if (condition 3)
Action 3
Else
Action 4
# awk-f: ' {if ($ = = "root") print $1;else if ($ = = "Seker") print $6;else if ($ = = "Zorro") Print $7;else print NR} '/ etc/passwd
Root
2
3
...
33
/home/seker
/bin/bash
36
Conditions? Action 1: Action 2
Expr?action1:action2
# awk-f: ' var= ($ >=) $: "System_user" {print $ \ t "$" \ T "var} '/etc/passwd
# awk-f: ' {print ($3>500?$1:$2)} '/etc/passwd
Linux/awk, variables, operators, if multi-branch