awk use (1)

wk[ -F re] [parameter...] ['prog'] [-f progfile][in_file...]

 

 

awk -F: '$1~/\<12345\>/{print}' abc.txt

 

Parameter description:

1
) -F re: allows awk to change its field separator.

2) parameter: 
This parameter helps assign values ​​to different variables. 3) 'prog': the program statement segment of

awk .
This statement segment must be enclosed in single quotes: ' and ' to prevent interpretation by the shell . The standard form of this program statement segment is: 'pattern{action}' where the pattern parameter can be any of the egrep regular expressions, with // including

, the action parameter is always surrounded by curly brackets, it consists of a system of awk statements, each statement is separated by ";" .

 

 

awk uses $0 to represent entire lines (records). The system default delimiter is space. Awk allows changing this delimiteron the command line with the form -F re . In fact, awk uses a built-invariable FS to memorize this delimiter.

 

Example : Display the first field, the third field and the seventh field separated by the character % in the seventh to fifteenth lines of the text file myfile : $awk -F % ' NR==7,NR==15 {printf $1 $3 $7}' myfile

 

 

$awk' /[Ss]un/, /[Mm]oon/ {print}' myfile

It will show the lines between the first line matchingSun or sun and the first line matching Moon or moon ( note: !! ) , and display it on standard output.

 

例:下面的示例显示了内置变量和内置函数length()的使用:

$awk'length($0)>80{print NR}' myfile

 

例:作为一个较为实际的例子,我们假设要对UNIX中的用户进行安全性检查,方法是考察/etc下的passwd文件,检查其中的passwd字段(第二字段)是否为"*",如不为"*",则表示该用户没有设置密码,显示出这些用户名(第一字段)。我们可以用如下语句实现:

#awk-F: '$2=="\*"{printf("%s no password!\n",$1}' /etc/passwd

awk中允许进行多种测试,如常用的==(等于)、!=(不等于)、>(大于)、<(小于)、>=(大于等于)、>=(小于等于)等等,同时,作为样式匹配,还提供了~(匹配于)和!~(不匹配于)判断

awk中两个特别的表达式,BEGINEND,这两者都可用于pattern中(参考前面的awk语法),提供BEGINEND的作用是给程序赋予初始状态和在程序结束之后执行一些扫尾的工作。任何在BEGIN之后列出的操作(在{}内)将在awk开始扫描输入之前执行,而END之后列出的操作将在扫描完全部的输入之后执行。因此,通常使用BEGIN来显示变量和预置(初始化)变量,使用END来输出最终结果。

例:累计销售文件xs中的销售金额(假设销售金额在记录的第三字段):

$awk
>'BEGIN{ FS=":";print "
统计销售金额";total=0}
>{print$3;total=total+$3;}
>END{printf "
销售金额总计:%.2f",total}' sx
(注:>shell提供的第二提示符,如要在shell程序awk语句和awk语言中换行,则需在行尾加反斜杠

 

^在字符串的开头开始匹配
$
在字符串的结尾开始匹配
.
与任何单个字符串匹配
[ABC]
[]内的任一字符匹配
[A-Ca-c]
A-Ca-c范围内的字符匹配(按字母表顺序)
[^ABC]
与除[]内的所有字符以外的任一字符匹配
Desk|Chair
DeskChair中的任一个匹配
[ABC][DEF]
关联。与ABC中的任一字符匹配,且其后要跟DEF中的任一个字符。
*
ABC中任一个出现0次或多次的字符相匹配
+
ABC中任何一个出现1次或多次的字符相匹配
与一个空串或ABC在任何一个字符相匹配
(Blue|Black)berry
合并常规表达式,与BlueberryBlackberry相匹配

\{\}

\<\>

\(\)

awk的内置函数

 

函数用途或返回值
------------------------------------------------
gsub(reg,string,target)
每次常规表达式reg匹配时替换target中的string
index(search,string)
返回stringsearch串的位置
length(string)
求串string中的字符个数
match(string,reg)
返回常规表达式reg匹配的string中的位置
printf(format,variable)
格式化输出,按format提供的格式输出变量variable
split(string,store,delim)
根据分界符delim,分解stringstore的数组元素
sprintf(format,variable)
返回一个包含基于format的格式化数据,variables是要放到串中的数据
strftime(format,timestamp)
返回一个基于format的日期或者时间串,timestmpsystime()函数返回的时间
sub(reg,string,target)
第一次当常规表达式reg匹配,替换target串中的字符串
substr(string,position,len)
返回一个以position开始len个字符的子串
tolower(string)
返回string中对应的小写字符
toupper(string)
返回string中对应的大写字符

srand(x)
初始化随机数发生器。如果忽略x,则使用system()
system()
返回自197011日以来经过的时间(按秒计算)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326724090&siteId=291194637