Linux基础-1-awk

awk --help:

[guoky@localhost ~]$ 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:
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
-m[fr] val
-O --optimize
-W compat --compat
-W copyleft --copyleft
-W copyright --copyright
-W dump-variables[=file]--dump-variables[=file]
-W exec=file --exec=file
-W gen-po --gen-po
-W help --help
-W lint[=fatal]--lint[=fatal]
-W lint-old --lint-old
-W non-decimal-data--non-decimal-data
-W profile[=file]--profile[=file]
-W posix --posix
-W re-interval--re-interval
-W source=program-text--source=program-text
-W traditional--traditional
-W usage --usage
-W use-lc-numeric--use-lc-numeric
-W version --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

新建文本内容用来测试:

[guoky@localhost ~]$ vi test_awk.txt 
[guoky@localhost ~]$ cat test_awk.txt 
#Ihis is a txt for testing the function and usage of 'awk'.
There is nothing userful in this file.
author:guoky
time:2015-12-11 12:12

用法1:

每行按空格或TAB分割,输出文本中的1、4项:

[guoky@localhost ~]$ awk '{print $1,$4}' test_awk.txt 
#Ihis txt
There userful
author:guoky 
time:2015-12-11 

用法2:

awk -v 设置变量

[guoky@localhost ~]$ awk -v a=1 '{print $1,$(a+2)}' test_awk.txt 
#Ihis a
There nothing
author:guoky 
time:2015-12-11

可设置多个变量

[guoky@localhost ~]$ awk -v a=1 -v b=2 -vc=变量3 '{print $a,$b,$3c}' test_awk.txt 
#Ihis is a变量3
There is nothing变量3
author:guoky  变量3
time:2015-12-11 12:12 变量3
  变量3

用法3:

-f progfile:允许awk调用并执行progfile程序文件,当然progfile必须是一个符合awk语法的程序文件。

用法4:

过滤第一列为There的行:

[guoky@localhost ~]$ awk '$1=="There"' test_awk.txt 
There is nothing userful in this file.

过滤第一列不是There的行,并展示第一、三列:

[guoky@localhost ~]$ awk '$1!="There"{print $1,$3}' test_awk.txt 
#Ihis a
author:guoky 
time:2015-12-11



awk的内置变量:


匹配:

文本中含有字符串 is 的行:

[guoky@localhost ~]$ awk '/is/' test_awk.txt 
#Ihis is a txt for testing the function and usage of 'awk'.
There is nothing userful in this file.


忽略大小写:

[guoky@localhost ~]$ awk 'BEGIN{IGNORECASE=1} /there/' test_awk.txt 
There is nothing userful in this file.


匹配第一列包含 e 的行:

[guoky@localhost ~]$ awk '$1 ~ /e/' test_awk.txt 
There is nothing userful in this file.
time:2015-12-11 12:12

匹配第一列不包含 e 的行:

[guoky@localhost ~]$ awk '$1 !~ /e/ {print $1}' test_awk.txt 
#Ihis
author:guoky









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值