Brief introduction
Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.
AWK has 3 different versions: AWK, Nawk, and gawk, which are not specifically described, generally referred to as the GNU version of awk, Gawk,gawk.
Awk has its name from the first letter of its founder Alfred Aho, Peter Weinberger and Brian Kernighan's surname. In fact, Awk does have its own language: The awk programming language, a three-bit creator has formally defined it as "style scanning and processing language." It allows you to create short programs that read input files, sort data, manipulate data, perform calculations on input, and generate reports, as well as countless other features.
How to use
'{pattern + action}' {filenames}
Although the operation can be complex, the syntax is always the same, where pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when a match is found. Curly braces ({}) do not need to always appear in the program, but they are used to group a series of instructions according to a particular pattern. pattern is the regular expression to be represented, surrounded by slashes.
The most basic function of the awk language is to browse and extract information in a file or string based on the specified rules, before awk extracts the information for additional text operations. A complete awk script is typically used to format the information in a text file.
Typically, awk is treated as a unit of a file's behavior. awk processes the text by executing the corresponding command for each line that receives the file.
Call awk
There are three ways of calling Awk
1. command-line modeawk [-F Field-separator]‘Commands' Input-File (s) where commands is the true awk command, [-F domain delimiter] is optional. Input-file (s) is the file to be processed. In awk, each line in a file, separated by a domain delimiter, is called a domain. In general, the default field delimiter is a space without naming-f the field delimiter. 2.shell script inserts all awk commands into a file and makes the awk program executable, The awk command interpreter then acts as the first line of the script and is called again by typing the script name. Equivalent to the first line of the shell script: #!/bin/sh can be replaced by: #!/bin/awk3awk-f awk-script-file Input-file (s) where the-f option loads the awk script in Awk-script-file, Input-file (s) is the same as above.
This chapter focuses on the command-line approach.
Getting Started instance
Suppose the output of Last-n 5 is as follows
[Email protected] ~]#Last-n5 <==Remove only the first five elements root pts/1192.168.1.100 Tue Feb1011:Still loggedInchRoot pts/1192.168.1.100 Tue Feb1000:46-02:28 (01:41) Root pts/1192.168.1.100 Mon Feb911:41-:(£) dmtsai pts/1 192.168. 1.100 Mon Feb 9 : All-one :(xx:) root tty1 Fri Sep 5 :
14:
ten (xx:)
If you only show the 5 most recently logged-in accounts
awk '{print '}'
Root
Root
Root
Dmtsai
Root
The awk workflow is this: reads a record with a ' \ n ' line break, then divides the record by the specified domain delimiter, fills the field, and $ $ represents all fields, representing the first field, $n representing the nth field. The default Domain delimiter is the "blank key" or "[tab] key", so the login user, $ $ means the login user IP, and so on.
If you just show/etc/passwd's account
#cat/etc/passwd | awk ':'{print $}' rootdaemonbinsys
This is an example of awk+action, where each line executes action{print $.
-f Specifies the domain delimiter as ': '.
If you only display the/ETC/PASSWD account and the shell of the account, and the account and the shell are split by tab
#cat/etc/passwd | awk ':'{print $ \ t ' $7}'root/bin/bashdaemon/bin/shbin/bin/sh sys/bin/sh
If you just show/etc/passwd's account and the shell of the account, and the account is separated by a comma from the shell, and the column name Name,shell is added to all rows, add "Blue,/bin/nosh" to the last line.
cat/etc/passwd | awk ':'BEGIN {print ' Name,shell '} {print $ ', ' $7} END {print ' Blue,/bin/nosh '}'name, shellroot,/bin/bashdaemon,/bin/shbin,/bin/shsys,/bin/sh .... Blue,/bin/nosh
The awk workflow is done by first executing the beging, then reading the file, reading a record with the/n line break, and then dividing the record by the specified field delimiter, populating the field, and $ $ representing all fields, representing the first field, $n representing the nth field, The action action corresponding to the execution pattern is then started. Then start reading the second record ... Until all the records have been read, the end operation is performed.
Search all rows with the root keyword/etc/passwd
#'/root/'/etc/passwdroot:x:0:0:root:/root:/bin/bash
This is an example of the use of pattern, which matches the line of pattern (this is root) to execute the action (without specifying an action, the default output of the contents of each row).
Search support for the regular, for example, root start: awk-f: '/^root/'/etc/passwd
Search all lines that have the root keyword/etc/passwd and display the corresponding shell
'/root/{print $7}'/etc/passwd /bin/bash
Action{print $7} is specified here.
awk built-in variables
Awk has many built-in variables for setting up environment information, which can be changed, and some of the most commonly used variables are given below.
ARGC command-line arguments argv command-line parameter arrangement environ support the use of system environment variables in queues filename awk browses the file name Fnr the number of records to browse files FS Set input field delimiter, equivalent to command line-F option NF Browse record number of fields nr Read records ofs output field delimiter ors Output record delimiter Rs control record delimiter
In addition, the $ variable refers to the entire record. $ $ represents the first field of the current row, which is the second field of the current row,...... And so on
Statistics/etc/passwd: File name, line number per line, number of columns per row, corresponding full line contents:
#awk-F‘:‘‘{print "filename:" filename ", linenumber:" NR ", Columns:" NF ", Linecontent:" $ "}'/etc/passwdfilename:/etc/Passwd,linenumber:1,columns:7,LINECONTENT:ROOT:X:0:0:root:/root:/bin/bashfilename:/etc/passwd,linenumber:2,columns: 7,linecontent:daemon:x: 1: 1:daemon:/usr/sbin:/bin/sh filename:/etc/passwd,linenumber:3,columns:7,linecontent:bin:x:2:2:bin:/bin:/bin/shfilename:/etc/passwd,linenumber:4,columns:7,linecontent:sys:x:< Span style= "color: #800080;" >3:3:sys:/dev:/bin/sh
Use printf instead of print to make your code more concise and easy to read
awk ':' {printf ("filename:%10s,linenumber:%s,columns:%s,linecontent:%s\n", filename,nr,nf,$0)}'/etc/passwd
Print and printf
The functions of print and printf two printouts are also available in awk.
The parameters of the print function can be variables, values, or strings. The string must be quoted in double quotation marks, and the arguments are separated by commas. If there are no commas, the parameters are concatenated together and cannot be distinguished. Here, the function of the comma is the same as the delimiter of the output file, except that the latter is a space.
The printf function, whose usage is basically similar to printf in the C language, can format strings, and when the output is complex, printf is more useful and the code more understandable.
Transferred from: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html
Summary of awk usage