Common awk commands (for conversion) and awk commands
Awk usage: awk 'pattern' {action }'Variable name meaningNumber of ARGC command line VariablesARGV command line meta ArrayFILENAME current input file nameNumber of records in the current FNR FileThe input field delimiter of FS. The default Delimiter is a space.RS inp
Linux Shell programming awk and linuxshell programming awk
Awk [-field-separator] 'commands' input-file (s)
Basic Mode
Awk-F ':'...
Use # to separate
Awk '{print $0}' a.txt
Print all
Awk '{print $1, $3}' a.txt
Print 1s
Reproduced from: http://bbs.chinaunix.net/viewthread.php? Tid = 691456
Awk usage: awk 'pattern' {action }'
Awk Environment Variables
Variable
Description
$ N
The Nth field of the current record. The fields are separated by FS.
$0
Complete input records.
ARGC
The number of command line parameters.
that awk supports processing of records and fields, where the processing of fields is not implemented by grep and SED, which is also
One of the reasons that awk is superior to both. In awk, the default is always to treat a row in a text file as a record, and one part of a row as a field in the record. In order to manipulate these different words
section,
variables2.1 awk built-in variable's record variable:Fs:field separator, the field delimiter used when reading the file;Rs:record separator, enter the text information using the line break;Ofs:output Filed Separator:Ors:output Row Separator:Awk-f:ofs= "#"Fs= ":"2.2 The data variables for awk built-in variables:Nr:the number of the input Records,awk command, and
the third column is 0 6th column is listen)$ Awk ' $3==0 $6== "LISTEN" ' netstat.txttcp 0 0 0.0.0.0:3306 0.0.0.0:* listentcp 0 0 0.0.0.0:80 0.0.0.0:* listentcp 0 0 127.0.0.1:9000 0.0.0.0:* listentcp 0 0: :: LISTEN:::* where the "= =" is the comparison operator. Other comparison opera
' {print NF} '/etc/grub.confShow the number of fields per rowawk ' {print $, $NF} '/etc/grub.confShow the first and last fields of each rowawk ' {print nr,$0} '/etc/grub.confDisplay the contents and line numbers of each rowAwk-f: ' begin{ofs= '---"}{print $1,$7} '/etc/passwdDisplays the first and seventh columns, separated by---awk ' begin{fs= ': "}/bash$/{print nr
, multi-file record incrementFNR is similar to NR, although multiple file records are not incremented, each file starts from 1\ t Tab\ n line breakdefining delimiters when FS beginRS Enter the record delimiter, which defaults to a newline character (that is, the text is entered as one line)~ match, not accurate compared to = =!~ mismatch, inaccurate comparisons= = equals, must be all equal, exact comparison! = does not equal, exact comparison Logic an
") print $0}' temp
OR link: awk '{if ($1 = "a" | $1 = "B") print $0}' temp
2.4. awk built-in variables:
ARGC
Number of command line parameters
NF
Number of browsing records
AGRV
Command line parameter arrangement
NR
Number of read records
ENVIRON
Support the use of system environment variables in the queue
OFS
") Print $0}' temp
Or link: awk '{if ($1 = "A" | $1 = "B") Print $0}' temp
2.4. awk built-in variables:
Argc
Number of command line parameters
NF
Number of browsing records
Agrv
Command line parameter arrangement
NR
Number of read records
Environ
Support the use of system environment variables in the queue
OFS
.
RS: Record separator (a line break by default ).
RSTART: the first position of the string matched by the match function.
SUBSEP: array subscript separator (default value: \ 034)
Built-in functions: Class C style usage;
Printf: format the output, such as awk '{printf "% 03d % s \ n", NR, $1}' myfile
N gsub (reg, string, target) replaces the string
N index (search, string) returns t
default value of FS is a single blank character (awk is interpreted as one or more spaces or tabs ). In the preceding special example, the default value of FS is exactly what you want.
There is no problem with complex regular expressions. For example, if your record is separated by the word "foo" and three digits, the following regular expression will help you properly parse the record:
FS="foo[0-9][0-9][0-9]"
Field quantity
The two v
." In this particular example, the default FS setting is exactly what you want!
Complex rule expressions are not a problem. Even if your records are delimited by the word "foo" followed by three digits, the following rule expression still allows the data to be parsed correctly:
Fs= "Foo[0-9][0-9][0-9]"
Number of fieldsThe two variables that we're going to discuss are usually not assigned values, but are used to read to get useful information about the input. The first is the NF variable, also
.
Pattern {action}
There are two special pattern, BEGIN and END. we know that awk is a command for processing text lines. Therefore, the number of executions of normal pattern {action} is the number of lines of the specified file currently. BEGIN and END are executed only once, and BEGIN is executed before processing all rows. END is executed after processing is complete.
Pattern: defines a rule. As long as it complies with this rule in a line of text
Awk built-in Function Analysis and awk built-in functionsI. mathematical functions
The following arithmetic functions perform the same operations as child routines with the same name in C:
Function Name
Description
Atan2 (y, x)
Returns the arc tangent of y/x.
Cos (x)
Returns the cosine of x. x is a radian.
Sin (x)
Returns the sine of x, which is a radian.
awk is a row processor: Compared to the advantages of screen processing, there is no memory overflow or slow processing when processing large files, usually used to format text informationThe awk processing process:Each row is processed in turn, and then the outputawk command form:awk [-f|-f|-v] ' begin{}//{command1; Command2} end{} ' file [-f|-f|-v] Large parameter,-F specify delimiter,-F call script,-v de
environment variable Table 1. AWK environment Variables
Variable
Describe
$n
The nth field of the current record, separated by FS between the fields.
$
The complete input record.
ARGC
The number of command-line arguments.
Argind
The location of the current file in the command line, starting at 0.
Argv
An array that c
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.