current file.
Fs
The field delimiter (the default is any space).
IGNORECASE
If true, the matching of the case is ignored.
Nf
The number of fields in the current record.
Nr
The current number of records.
Ofmt
The output format of the number (the default value is%.6g).
OFS
The Output field delimiter (the default value is a space).
1} Condition Type 2 {Action 2 }.... End {post-processing }'The statements in begin and end play a role before and after reading the file (in_file). They can be understood as initialization and scanning.(1) parameter description:-F re: Allows awk to change its field separator-V Var = $ V: Assign the value of V to var. If multiple variables need to be assigned a value, write multiple-v values. Each variable corresponds to one-V value.E.g. Print the row
Is this a book
4. Awk common built-in variable tables:
1 $0 current record (as a single variable)
2 $1 ~ $ N the nth field of the current record. The fields are separated by FS.
3. The default delimiter of the FS input field is space.
4. The number of fields in the current NF record is the number of columns.
5 The number of records that NR has read, that is, the row number, starting from 1
6. The record i
Awk usage: awk 'pattern' {action} 'variable name meaning ARGC command line variable number ARGV command line variable element array FILENAME current input file name FNR current file record number FS input domain separator, default is a space RS input record delimiter NF number of fields in the current record NR number of records so far OFS output domain separator
separator when entering
Awk 'in in {FS = ":"} {print $1, $7} '/etc/passwd => of course, this writing method is really inferior to awk-F: '{print $1, $7}'/etc/passwd
RS: Record seperator, output line Separator
OFS: output field seperator, which is the field separator for output;
[[Email protected] _ 6 data] # awk 'in in {FS = ":"; OFS = "*"} {print $1, $7} '/etc/
timesX{m,}:x at least repeat m timesX{,m}:x up to repeat m timesX{m,n}:x repeats at least m times, but not more than n times[[emailprotected] tmp]# cat awk1rootrrttgood[[emailprotected] tmp]# awk --posix ‘/o{2}/{print $0}‘ awk1rootgood[[emailprotected] tmp]#2、关系表达式: Operation with operator, can be a string or numeric comparison test
,
[[emailprotected] tmp]# awk ‘NRgt;=2{print $0}‘ awk1rrttgo
Multi-dimensional awk array and multi-dimensional awk
Although the awk command does not claim to support multi-dimensional arrays, it provides a method to define multi-dimensional arrays. Awk defines multi-dimensional arrays by concatenating multiple subscripts into strings. subscripts are separated by the value of the
# Boolean variable. If it is true, case-insensitive matching is performed.
NF # number of domains in the current record
NR # Number of current records
OFMT # digital output format
OFS # output domain separator, default is Space key
ORS # output record delimiter, which is a line break by default
RLENGTH # String Length matched by the match Function
RS # record separator. The default value is Space key.
RSTART # The 1st position of the string matched b
Multi-file handling for awk:AWK's data input has two sources, standard inputs and files, and the latter way supports multiple files, such as1. Shell's pathname expansion way: awk ' {...} ' *.txt # *.txt is interpreted by the shell first, replaced by all *.txt in the current directory, such as 1.txt and 2.txt in the current directory, then the command ends with awk ' {...} ' 1.txt 2.txt2. Specify multiple fi
SeparatorORS output record SeparatorRS control record delimiter
In addition, the 0 variable refers to the entire record. The value 0 indicates the entire record. 1 indicates the first domain of the current row, $2 indicates the second domain of the current row, and so on.
Statistics/etc/passwd: file name, row number of each row, column number of each row, corresponding to the complete row content:
# Awk-F': ''{print" filename: "FILENAME", linenumber:
= $v assigns a V value to Var, and if there are multiple variables to be assigned, write multiple-V, and each variable assignment corresponds to a-Ve.g. to print the line between the NUM line of file A and the Num+num1 line,Awk-v num= $num-v num1= $num 1 ' nr==num,nr==num+num1{print} ' a-F Progfile: Allows awk to invok
======Basic article======1. An action instance without patternA awk ' {print NR $ $NF} ' data.txt prints line numbers, first and last columns, no separators in middleb awk ' {print $, $NF} ' data.txt prints the first and last columns, with delimiters in the middleC awk ' {print$0, $NF +10} ' data.txt prints the entire
each row is output by default ).
Regular Expressions are supported in search, for example, awk-F: '/^ root/'/etc/passwd.
Search for all rows with the root keyword in/etc/passwd and display the corresponding shell
# Awk-F: '/root/{print $7}'/etc/passwd
/Bin/bash
Action {print $7} is specified here}
Awk built-in VariablesAwk has many built-in variables used to set
. built-in variables of awk
Common variables of awk include:NF: How many segments are separated by delimitersNR: number of rows{Print NR ":" NF} lists the row numbers separated by colons to list the total number of segments;
[Root @ bkjia ~] # Head-5 passwd | awk-F: '{print NR
the currently processed file awk '{if (ARGIND = 1) {print $1} if (ARGIND = 2) {print $2} 'aaa.txt bbb.txt # first scans the aaa file and then the bbb File
Number of records read by NR awk 'nr = 1, NR = 5 {print} 'aaa.txt # displays 1 to 5 rows of aaa.txt files
FNR current f
start with root: awk-f: '/^root/'/etc/passwd
Search for all lines with the root keyword in the/etc/passwd and display the corresponding shell
The code is as follows:
# awk-f: '/root/{print $} '/etc/passwd
/bin/bash
Action{print $} was specified here
awk Built-in variables
Awk has a number of built-in variables
parameter arrangement environ support the use of system environment variables in queues filename awk Browse by file name fnr number of records to browse files fs set input field delimiter, equivalent to command line -f option nf number of fields that are logged nr number of records read ofs output Field delimiter ors Output Record delimiter rs Control record Separators In addition, the $ variable
awk commandawk, like SED, is a streaming editor that operates on lines in the document, one line at a line. Awk is more powerful than SED, and it can do what sed can do. The awk tool is actually very complex and has special books to describe its use.1 awk Command Formawk [-f|-f|-v] ' begin{}//{command1; Command2} end{}
the queueFilename awk browsed file name FNR browsed file record count FS sets input domain separator, same as-F OptionNumber of NF browsing records Nr number of records read OFS output domain SeparatorORS output record separator Rs control record SeparatorExample: awk 'end {print Nr} 'temp prints the number of read re
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.