Linux /awk/ Usage

<?xml version="1.0" encoding="utf-8"?> Linux awk Usage

Linux awk Usage

In this post, I am going to record the common patterns of using awk, the amazing tool for processing file.
Assume I have a file (bb) with the following contents:
70 23 2 757 35 3 765 56 3 765 56 1 23 4 11 3

1 Sum a specific column:

awk '{sum += $1} END {print sum}' bb

2 Sum the columns of the lines with additional conditions(starting with 7):

1: awk '/^7/ {print $2}' bb | awk '{sum += $1} END {print sum}'

3 Output the line whose value in the 3rd column is 1:

1: awk '$3 == 1' bb

4 Output the line whose $1 < 80 and $2 > 20:

1: awk '{$1<80 && $2>20;print $3}' bb

5 Count the lines starting with 7:

1: awk '/^7/ {x++;} END {print x}' bb

6 Output the line containg number 765

1: awk '/765/' bb

7 Output the line containing number 765 with the line number in front of that line:

1: awk '/765/ {print NR":"$0}' bb

In the above command, the NR is the built-in variable that represents the line being processed, the $0 refers to the whole line.9

8 Output the lines not containing number 765:

1: awk '!/765/' bb

Author: wujing

Created: 2015-01-23 五 21:22

Emacs 24.3.1 (Org mode 8.2.10)

转载于:https://www.cnblogs.com/wujingcqu/p/4245084.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值