How to use
awk [-F Separator] ' handling of operations ' is the file name to be processed
It can be separated by a space without a separator.
Three ways to execute awk
1. Command mode
2.Shell scripting #!/bin/awk on the first line
3. Insert all awk commands into a single file using the awk - F awk command file with the processing file
A few simple examples
1. Get the most recently logged in user name simple print
Last - N 5 | awk ' {print '} '
2. Get/etc/passwd account Specify the delimiter
$ indicates that all the domains , $ represents the first domain , $n represents the first N a domain
CAT/ETC/PASSWD |awk-f ': ' {print $} '
can also
Awk-f ': ' {print $} '/etc/passwd
Print multiple columns
awk-f ': ' {print $ \ t ' $7} '/etc/passwd
3. Use Begin end
Example 1
CAT/ETC/PASSWD |awk-f ': ' BEGIN {print ' Name,shell '} {print $ ', ' $7} END {print ' Blue,/bin/nosh '} '
Example 2
Awk-f ":" ' begin{count=0;} {name[count]=$1;count++} End{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd
4. Using the regular
Example 1 View all columns that contain ysql
Awk-f ":" ' begin{count=0;} /ysql/{name[count]=$1;count++}end{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd
Example 2 View all columns beginning with Ysql
Awk-f ":" ' begin{count=0;} /^ysql/{name[count]=$1;count++}end{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd
Example 3 View all columns ending in Ysql
Awk-f ":" ' begin{count=0;} /ysql$/{name[count]=$1;count++}end{for (i=0;i<count;i++) print I,name[i]} '/etc/passwd
Note that the regular must be placed behind the begin
Awk's use case