linux awk 使用


linux awk 使用

 

 

**********************

awk 简介

 

awk是文本行分析处理命令,用法如下:

[root@centos test]# awk --help
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:		GNU long options: (standard)
	-f progfile		--file=progfile
	-F fs			--field-separator=fs
	-v var=val		--assign=var=val
Short options:		GNU long options: (extensions)
	-b			--characters-as-bytes
	-c			--traditional
	-C			--copyright
	-d[file]		--dump-variables[=file]
	-e 'program-text'	--source='program-text'
	-E file			--exec=file
	-g			--gen-pot
	-h			--help
	-L [fatal]		--lint[=fatal]
	-n			--non-decimal-data
	-N			--use-lc-numeric
	-O			--optimize
	-p[file]		--profile[=file]
	-P			--posix
	-r			--re-interval
	-S			--sandbox
	-t			--lint-old
	-V			--version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.
#默认从标准输入读取数据,输出到标准输出

Examples:
	gawk '{ sum += $1 }; END { print sum }' file
	gawk -F: '{ print $1 }' /etc/passwd

[--]:命令行中写入的要执行的awk操作,如 "{print $1}"(也可写入脚本a.awk内,使用-f a.awk)

 

常用选项:

-f :指定执行的awk脚本,如 -f a.awk

-v:设置自定义变量,如 -v a=1

-F:设置分隔符,默认为空格

 

常用内置变量

$0:全部文本行

$1:文本行指定分隔符分隔后的第一个字段

$n:文本行指定分隔符分隔后的第n个字段

 

FILENAME:文件的名字(区分大小写)

IGNORECASE:是否忽略大小写,设置为非0忽略大小写,默认区分大小写

 

FS(field seperator):输入字段的分隔符

OFS(out field seperator):输出字段的分隔符

 

RS(record seperator):行记录分隔符

ORS(output record seperator):输出行记录分隔符

 

NF(number of field):分隔后的字段数目

NR(number of record):问本行在所有文件中的行号

FNR(file number of record):文本行在当前文件中的行号

 

 

常用函数:

length:文本行的长度

length($1):字段1的长度

 

 

数组:

定义数组:array_name[index]=value,如果不设置value,默认为0

删除数组:delete array_name
删除数组元素:delete array_name[index]

 

 

**********************

示例

 

IGNORECASE

[root@centos ~]# echo hello HELLO|awk '($1 ~ /hello/) && ($2 ~ /hello/)   {print $1 " " $2}'
[root@centos ~]# echo hello HELLO|awk 'BEGIN {IGNORECASE=1} ($1 ~ /hello/) && ($2 ~ /hello/)   {print $1 " " $2}'
hello HELLO
[root@centos ~]# echo hello HELLO|awk 'BEGIN {IGNORECASE=2} ($1 ~ /hello/) && ($2 ~ /hello/)   {print $1 " " $2}'
hello HELLO

 

-F "=":指定分隔符为=

-F "[;=]":先按照";"分隔,再按照"="分隔

#使用默认的分隔符
[root@centos test]# echo "hello hello2 hello3 hello4"|awk '{print $1, " ", $2, " ", $3, " ", $4}'
hello   hello2   hello3   hello4

#使用分隔符 “=”
[root@centos test]# echo "hello=hello2=hello3=hello4"|awk '{print $1, " ", $2, " ", $3, " ", $4}'
hello=hello2=hello3=hello4         
[root@centos test]# echo "hello=hello2=hello3=hello4"|awk -F "=" '{print $1, " ", $2, " ", $3, " ", $4}'
hello   hello2   hello3   hello4
[root@centos test]# echo "hello = hello2 = hello3 = hello4"|awk -F "=" '{print $1, " ", $2, " ", $3, " ", $4}'
hello     hello2     hello3     hello4

#指定多分隔符 “[;=]”
[root@centos test]# echo "key=value;key2=value2"|awk -F "[;=]" '{print $1 " " $2, "\n" $3 " " $4}'
key value 
key2 value2

 

正则匹配:~、!~

#文本行匹配正则表达式:含有hello
[root@centos test]# echo "hello HELLO"|awk '/hello/ {print $1}'
hello

#指定字段匹配正则表达式
[root@centos test]# echo "hello HELLO"|awk '$1 ~ /hello/ {print $1}'
hello
[root@centos test]# echo "hello HELLO"|awk '$2 ~ /hello/ {print $1}'

#指定字段不匹配正则表达式
[root@centos test]# echo "hello HELLO"|awk '$1 !~ /hello/ {print $1}'
[root@centos test]# echo "hello HELLO"|awk '$2 !~ /hello/ {print $1}'
hello

 

数字运算

[root@centos test]# echo "1 2 3"|awk '$1==1 {print $1,$2,$3+2}'
1 2 5

[root@centos test]# echo "1 2"|awk '$1<$2 {print $1,$2}'
1 2

[root@centos test]# echo "1 2"|awk '($1<$2)&&($2<4) {print $1,$2}'
1 2

 

-f a.awk:指定执行的脚本

[root@centos test]# cat a.awk
$1>$2 {print $1,$2}
[root@centos test]# echo "1 0"|awk -f a.awk
1 0


[root@centos test]# cat b.awk
BEGIN {
  a=1;
  b=2;
}  #开始执行时执行一次

{
  print $1+a,$2+b
}  #对每个文本行都执行

END {
  print "end"
}  #最后执行一次
[root@centos test]# echo "1 2"|awk -f b.awk
2 4
end

注意:BEGIN、END区分大小写

 

length:长度计算

[root@centos test]# echo "hello hello2"|awk '{print length, length($0), length($1), length($2)}'
12 12 5 6

 

 

**********************

awk 条件语句

 

语法格式

#if 单条语句
if (condition)
  do


#if-else 复合语句
if (condition)
  do
else
  do

#if-else if 复合语句
if (condition)
  do
else if (condition)
  do
else
  do

 

示例

[root@centos test]# cat c.awk
{if($1<$2){
  print $1 " 小于 " $2
} else if($1==$2){
  print $1 " 等于 " $2
} else {
  $1 " 等于 " $2
}
} 
[root@centos test]# echo "1 2"|awk -f c.awk
1 小于 2

注意:if -else语句需用“{}”

 

 

**********************

awk 循环语句

 

语法格式

for(expression1;expression2;expression3){
  do
}


#遍历数组
for(var in array){
  do
}


#while循环
while(expression){
  do
}

 

示例

[root@centos test]# cat 2.awk
{
  for(i=1;i<=6;i++){
    print $1+i;
  }
}
[root@centos test]# echo "1"|awk -f 2.awk
2
3
4
5
6
7

 

示例 2

[root@centos test]# cat 3.awk
BEGIN {
  a["1"]=1
  a["2"]=2
  {
    for(i in a){
      print i "==>" a[i]
    }  
  } 
}
[root@centos test]# awk -f 3.awk
1==>1
2==>2

 

示例 3

[root@centos test]# cat 4.awk
BEGIN {
  i=1;
  while(i<=6){
    print i;
    i++;
  }
}
[root@centos test]# awk -f 4.awk
1
2
3
4
5
6

 

 

**********************

awk 循环控制语句

 

continue:跳过后面部分继续执行

break:结束循环

 

示例

[root@centos test]# cat 5.awk
BEGIN {
  i=0;
  sum=0
  while(i<=10){
    sum+=i;
    i++;

    if(sum>=20){
      print i;
      break;
    }
  }
}
[root@centos test]# awk -f 5.awk
7

 

示例 2

[root@centos test]# cat 6.awk
BEGIN {
  i=0;
  while(i++<=10){
    if(i%2!=0){
      continue;
    }

    print i;
  }
}
[root@centos test]# awk -f 6.awk
2
4
6
8
10

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值