AWK 小结

# 介绍
Awk是一门脚本语言,在linux下面有一个awk工具,一般配合sed工具使用
> The AWK Programming Language(book)


# Awk处理流程
1. Awk reads all input either from standard input or files.
2. The input is read in unit called records, default is a line
3. Each record will processed by rule of the program, and will split to chunks called fields.


# Awk关键字


变量 | 定义
-----|------
$0   | 整个record(默认为一行)
$1~$n | 第n个filed
NF   | 当前record的有多少个filed
NR   | 当前文件第几个record
FNR  | 所有文件的第几个record
FS   | 输入filed的分隔符,默认为空格
RS   | 输入record的分隔符,默认为空行




# Awk语法
- BEGIN{action //here put the code that will processed before read record like define some variables, if not need, ignore it }
- //match pattern {action//here put the code will be processed in every record}  (Can be more than ones)
- END {action //here put the code will be processed after all record all processed, if not need, ignore it }


# 用例
## 用例1
awk '{print $3, $9, $5 }'  
awk 'NR!=1 {print $3, $9, $5 }'   
awk 'NR!=1 {printf "%-10s %-25s %10s\n",$3, $9, $5 } '  
awk 'NR!=1 && $3=="ewajinx" {printf "%-10s %-25s %10s\n",$3, $9, $5 }'
## 用例2
awk 'BEGIN{print "calculating ....."} {sum+=$5} END {print sum}'
## 用例3
可以使用-F指定其他分割符.  
cat file.txt | awk -F, '{print $1 "," $3 }'这里我们使用,作为字段分割符,同时打印第一个和第三个字段。  如果该文件内容如下: Adam Bor, 34, IndiaKerry Miller, 22, USA
命令输出结果为:Adam Bor, IndiaKerry Miller

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值