当前位置:首页 > 操作系统 > Linux

linux awk命令使用的一些心得--参数和内置属性

1、内置变量

NF 当前行有多少片,则 $NF 表示当前行的最后一个值  

例子:

#awk ‘{print $NF}‘ 1 ;  //打印一行的倒数第一个值
#awk ‘{print $(NF-1)}‘ 1  //打印一行的倒数第二个值


FS 输入的段分隔符  

#awk -v FS=: ‘{print $1}‘ /etc/passwd   //   -v 定义一个变量


OFS 输出的段分隔符

#awk -v OFS=: ‘{print $1,$2}‘ /etc/passwd

2、操作符

算术操作符

-x : 负值

+x :转化为数值

x ^y x y 次方

x**y:x y 次方

x*y:x 乘以 y

x/y x 除以 y

x+y

x-y

x%y :取余

 

字符操作符

只有一个,用于字符串连接

linux1:/home # awk ‘BEGIN{print "a""b"}‘
ab
linux1:/home # awk ‘{print "a""b"}‘
ab
 
ab

没有 begin 关键字,则需要回车后,才会显示,而且不会退出

 

 

赋值操作符

=

+=

-=

*=

/=

%=

^=

**=

++

--

如果某模式为 = 号,应以 /[=]/ 替代

 

布尔值

任何非 0 值或非空字符串都为真,反之为假,跟 bash 中刚好相反

比较操作符

x<y 

x<=y

x>y

x>=y

x==y

x!=y

x~y:y 是模式, x 的字符串能否被 y 匹配到

linux1:/home # awk -F: ‘$1 ~ /^root/ {print $3,$4,$NF}‘ /etc/passwd
0 0 /bin/bash

$1 符合 /^root/ 匹配的行


x!~y:x 不能被 y 模式匹配到则为真

表达式间逻辑

&&

||

条件表达式

Selector?if-true-exp:if-false-exp

A= 3

B=4

$A>$B:echo $A:echo $B

函数调用

Function_name (para1,para2)


3、模式用法-pattern

常见类型

Regexp :正则表达式   /regexp/, 需要用 / / 包括

expresssion :表达式,其值非 0 或非空字符串

Awk -F: ‘$3>=500 {print $1 $3}‘ /etc/passwd

Ranges: 指定匹配范围,格式为 pat 1 pat2

匹配符合 pat1 的行到符合 pat2 的行范围内的行

BEGIN/END 在真正的 action 前或后只执行一次

linux1:/home # awk -F: ‘BEGIN{print "Username   is"}$3>500 {printf "%-15s%sn", $1,$3}END{print "file is end"}‘ /etc/passwd
Username    is
nobody         65534
sybase         1000
exbadm         1001
tcqs           1002
tomcat         1003
dasusr1        1004
db2inst1       1005
db2fenc1       1006
iccs           1007
chenjie        1008
uftp           1009
inter1         1010
file is end

可在 BEGIN 中变量赋值

#awk ‘BEGIN{FS=":"}{print $1}‘ /etc/passwd

空模式:匹配任意输入行


4、 action

Expressions

 

Control statement

If-else

语法:

if(condition){then-body} else {else body}
linux1:/home # awk -F: ‘{if ($3==0) print $1,"Admin";else print $1,"Common
User"}‘ /etc/passwd
root Admin
bin Common User
daemon Common User
lp Common User
mail Common User
games Common User
wwwrun Common User

 

While

语法:

While (condition){statement;statemnet2;…}

linux1:/home #awk -F:‘{i=1;while(i<=3) {print $i;i++)}‘ /etc/passwd
linux1:/home # awk -F: ‘{i=1;while (i<=3){printf "%-15s", $i;if(i==3){printf
"n"};i++}}‘ /etc/passwd
root           x              0             
bin            x              1             
daemon         x              2             
lp             x              4             
mail           x              8             
games          x              12

 

Do-while

语法:

Do {statement 1,statement2} while(condition)

至少会执行一次

For

#awk -F:‘{for(i=1;i<=NF;i+=2) print $1}‘ /etc/passwd

for 遍历

Case

break continue

 

Compound statement 函数调用

Input statement

Output statement


5、数组

Array [index-expression]

index-expression 可以是任意字符

引用时不需要用 $

linux1:/home # awk ‘BEGIN{A["x"]="hello";A["y"]="world";print A["x"],A["y"]}‘
hello world

 

要遍历数组,使用

For(A in array){print array[A]},A array 的下标,如果下标为数字,则不需要双引号

linux1:/home # awk ‘BEGIN{A["x"]="hello";A["y"]="world";for(B in A) print A[B]}‘
hello
world

例子

netstat -ant| awk ‘$1~/tcp/{S[$NF]++}END{for (A in S){print A,S[A]}}‘
LISTEN 19
ESTABLISHED 21
TIME_WAIT 2

 

显示 netstat -ant 出来的状态,统计不同状态的个数

6、内置函数

Split(String,array[,fieldsep[,seps]])

string 标的的字符串以 fieldsep 为分隔符进行分隔,并将分隔后的结果保存至 array 为名的数组中

linux1:/home #netstat -ant|awk ‘/:80/{split($5,clients,":");IP[clients[1]]++}END{for( i in IP){print IP[i],i}}‘
3 0.0.0.0

访问 80端口的ip链接及数量


Length ([string]) 返回 string 字符串中字符的个数;

 

Substr (string,start[,length]) string 字符串中的子串,从 start 开始,取 length 个, start 1 开始计数

 

System (command) 执行系统命令并将结果返回至 awk

systime () 取系统当前时间


本文出自 “测试的律动” 博客,请务必保留此出处http://fociceo.blog.51cto.com/2480/1664463

原文:http://fociceo.blog.51cto.com/2480/1664463


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐