标签:mongo $1 awk命令 格式化 top apple ike cas 输出
参考菜鸟教程
AWK 是一种处理文本文件的语言,是一个强大的文本分析工具。
之所以叫 AWK 是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首字符。
awk [选项参数] ‘script‘ var=value file(s)
或
awk [选项参数] -f scriptfile var=value file(s)
每行按空格或TAB分割,输出文本中的1、4项
[root@node1 ~]# cat log.txt
2 this is a test
3 Are you like awk
This‘s a test
10 There are orange,apple,mongo
[root@node1 ~]# awk ‘{print $1, $4}‘ log.txt # 行匹配语句 awk ‘‘ 只能用单引号
2 a
3 like
This‘s
10 orange,apple,mongo
[root@node1 ~]#
[root@node1 ~]# awk ‘{printf "%-8s, %-10s\n", $1, $4}‘ log.txt # 格式化输出
2 , a
3 , like
This‘s ,
10 , orange,apple,mongo
awk -F #-F相当于内置变量FS, 指定分割字符
使用多个分隔符.先使用空格分割,然后对分割结果再使用","分割
[root@node1 ~]# cat log.txt
2 this is a test
3 Are you like awk
This‘s a test
10 There are orange,apple,mongo
[root@node1 ~]# awk -F ‘[ ,]‘ ‘{print $1,$2,$5}‘ log.txt
2 this test
3 Are awk
This‘s a
10 There apple
awk -v # 设置变量
定义了两个变量,一个是a=1,一个是b=s
[root@node1 ~]# awk -va=1 -vb=s ‘{print $1, $1+a, $1b}‘ log.txt
2 3 2s
3 4 3s
This‘s 1 This‘ss
10 11 10s
例子: 过滤出第一列大于2,并且第二列等于Are的
[root@learn200 ~]# awk ‘$1>2 && $2="Are" {print $1,$2,$3}‘ log.txt
3 Are you
This‘s Are test
There Are orange,apple,mongo
~ 表示模式开始。// 中是模式。
[root@learn200 ~]# cat log.txt
2 this is a test
3 Are you like awk
This‘s a test 10
There are orange,apple,mongo
[root@learn200 ~]# awk ‘/re/‘ log.txt
3 Are you like awk
There are orange,apple,mongo
[root@learn200 ~]# awk ‘$2 ~ /re/ {print $2}‘ log.txt
Are
are
[root@learn200 ~]#
[root@learn200 ~]# awk ‘BEGIN{IGNORECASE=1} /this/‘ log.txt
2 this is a test
This‘s a test 10
[root@learn200 ~]#
[root@learn200 ~]# cat log.txt
2 this is a test
3 Are you like awk
This‘s a test 10
There are orange,apple,mongo
[root@learn200 ~]#
[root@learn200 ~]# awk ‘$2 !~ /th/‘ log.txt
3 Are you like awk
This‘s a test 10
There are orange,apple,mongo
[root@learn200 ~]#
[root@learn200 ~]# awk ‘!/th/‘ log.txt
3 Are you like awk
This‘s a test 10
There are orange,apple,mongo
[root@learn200 ~]#
[root@learn200 ~]# ls -l *.txt
-rw-r--r--. 1 root root 182 Oct 15 2019 cookies.txt
-rw-r--r--. 1 root root 85 Jun 28 22:32 log.txt
-rw-r--r--. 1 root root 17391 Dec 21 2019 top.txt
[root@learn200 ~]# ls -l *.txt | awk ‘{sum+=$5} END {print sum}‘
17658
[root@learn200 ~]#
[root@learn200 ~]# ll *.txt | awk ‘BEGIN {sum = -1000} {sum+=$5} END{print sum}‘
16658
标签:mongo $1 awk命令 格式化 top apple ike cas 输出
原文地址:https://www.cnblogs.com/AganRun/p/13205629.html