Similar calls awk sed
(current linux are used in gawk, both now equivalent to each other a.)
Executing a statement in awk, similar to C / C ++
awk call in three ways
① Enter the shell command
awk [-F 域分隔符] 'awk 程序段' 输入文件
②awk block insert script file, and then call it command awk
awk -f awk脚本文件 输入文件
#通过-f 调用awk脚本文件
③ After the awk command into a script file, set the executable, and then execute the script file
./awk脚本文件 输入文件
1. awk pattern matching
awk action by the pattern and composition;
pattern mode selection input line to perform follow-up action if action;
action action may be included during the execution of statements, functions, expressions;
pattern supported by awk regular representation Dayuan character than grep, wide sed;
# Print blank lines
awk '/^$/{print "This is a blank line."}' input
2. awk records and fields
awk Human input is a structured document, awk each input line is defined as a record file;
each row is defined as a string field, divided by a space between the domains, Tab key, or other symbols;
a plurality of consecutive spaces, multiple Tab between consecutive symbol processing and the like as a domain segmentation;
Domain, $ $ later use awk number represents the specified take the first execution of several fields, starting from index 1, $ 1 to obtain the value of the first field;
$ 0 for all domains;
3. awk variable system
variable name | significance |
---|---|
$n | Takes a value of n-th field |
$0 | Take all domains, i.e. the entire line |
ARGC | The number of command line arguments |
ARGIND | The command line of the current file location (starting from 0 reference numeral) |
ARGV | An array of command line arguments |
CONVFMT | Digital conversion format |
ABOUT | Environment Variables associative array |
ERRNO | Finally, a description of the error system |
FIELDWIDTHS | The width of the field list to split spacebar |
FILENAME | Current file name |
FNR | Record number browse files |
FS | Field separator, default spacebar |
IGNORECASE | Boolean variable, if true, ignore case matching is performed |
NF | The number of domains in the current record |
NO | The current number of records |
OFMT | Digital output format |
OFS | Output field separator, default spacebar |
ORS | Output record separator, default newline |
RLENGTH | By the match function matched string length |
RS | Record delimiter, default linefeed |
RSTART | A matched string match function is lower than the position of the bus |
SUBSEP | Array subscript separator, the default value is \ 034 |
4. awk formatted output
printf (格式控制符,参数)
Modifiers:
Modifiers | significance |
---|---|
- | Left |
width | Step domain |
.prec | Digits to the right of the decimal point |
Formatter:
Formatter | |
---|---|
%c | ASCII characters |
%d | Integer |
%e | Floating point, scientific notation |
%f | Float |
%O | Octal |
%s | String |
%x | Hexadecimal number |
awk 'BEGIN {FS=","} {printf("%s\t%d\n",$2,$8)}' sturecord
#配合修饰符
awk 'BEGIN {FS==" ";print "Name\t\tPhonenumber"} {printf("%-15s\t%s\n",$1,$2)}' sturecord
5. awk built-in string functions
function | |
---|---|
gsub(r,s) | Alternatively the input file with r s |
gsub(r,s,t) | Alternatively r with s at t |
index(s,t) | Returns the position of the first string of t s |
length(s) | |
match(s,t) | S test if the replacement string matching t |
split(r,s,t) | On into the sequence s t r |
sub(r,s,t) | r t on the first occurrence of s is replaced |
substr(r,s) | Returns suffix r partial string s starting from |
substr(r,s,t) | Returns a string from r s t start portion of the length of the suffix |
6. awk script file to transfer transmission
awk脚本 parameter=value 输入文件
./pass.awk MAX=3 FS="," inpufilename
Note that the command-line parameters can not be accessed statement BEGIN field
7. awk conditional statements and loop statements
if (x ~ /[Hh]el?o/) print x
do
动作
while (天剑表达式)
for (设置计数器初值;测试计数器;计数器变化)
动作
8. awk array array [index] = value
Associative array
8. awk practical operation of the recording
Enter the file name is sturecord :( spaces are behind the Tab key)
Li Hao njue 025-83481010
Zhang Ju nju 025-83466534
Wang seu 025-83494883 the Bin
Zhu Lin njupt 025-83680010
#awk 打印第一、二列域
awk '{print $1,$2}' sturecord
#awk 变量指定域号
awk 'begin {one=1;two=2} {print $(one+two)}' sturecord
#-F 改变分隔符
awk -F "\t" 'print $3' sturecord
#awk 同样也提供另一个环境变量FS的改变分割符
awk 'BEGIN {FS=","} {print $0}' sturecord
awk 'BEGIN {FS=","} {print $1,$3}' sturecord
#awk 中 ~匹配正则表达式 !~ 不匹配正则表达式
awk 'BEGIN {FS=":"} $1~/root/' /etc/passwd
#if/else else 以及与或运算,用逗号分割,第三列等于10或第四列等于10则打印整行
awk 'BEGIN {FS=","} {if ($3==10 || $4==10) print $0}' /etc/paswd
#awk 统计 空行,类似C/C++ 有x++ ++x的区别
awk '/^$/{print x+=1}' input