awk 常用方法

awk's usage

awk -F "spearator" '[BEGIN {}] [{}] [END {}]' file1 file2 *.txt

example
#---- Sperator -----
00. awk '{print $1}' info.txt
01. awk -F ";" '{print $0}' info.txt
02. awk -F "[,/t]" '{print $2, $3}' info.txt
03. awk -F "[,/t]" '{if( $1 > 100) print $2, $3;}' info.txt

#---- BEGIN and END -----
04. awk -F ",/t" 'BEGIN {num = 0;} {num++;} END { printf "total=%s/n", $num }' info.txt
05. awk -F ",/t" 'BEGIN {num = 0;} {print num++;} ' info.txt

#---- Regular Expressions -----
05. awk '/abc/ { print $1, $3} *.txt
06. awk '/abc/ { printf "find %s/n", $0}
        /123/ { printf "find %s/n", $1}' *.txt
07. awk '/abc/ || /123/ { print $1, $3} *.txt
08. awk '/abc/ && /123/ { print $1, $3} *.txt
09. awk -F "[,;]" '/abc/ {print hello,$0} ; /123/ {print world,$0}' xxx.txt
10. awk '{ if ($1 ~ /Jack/) print $0 }'         # ~  -> $1 match "Jack"
11. awk '{ if ($1 !~ /Jack/) print $0 }'        # !~ -> $1 not match "Jack"

#条件判断

awk  '{if( $4 == "false") print FILENAME,$1;}' data.txt

#搜索多个目录

awk  '{if( $4 == "false") print FILENAME,$1;}' /data/*/*.csv /data2/*/*.csv

#---- Build Variable -----
NF: the last field in a record
RS: character can be used for the record separator
NR: the total number of input records read so far from all data files
FNR: the number of records that have been read so far from the current input file

FILENAME:当前文件名

#打印指定N列后的所有列

awk ‘{ for(i=1; i<=1; i++){ $i="" }; print $0 }’ test.txt

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值