awk常见基本使用
-F 指定分割符号
print 外层的引号必须是单引号 $n不能被解析
[root@bogon ~]# cat 1.txt a:b:c:d a1:b1:c1:d1 a2:b2:c2:d2 a_: :c:dddd [root@bogon ~]# awk -F ':' '{print $1}' 1.txt a a1 a2 a_ [root@bogon ~]# awk -F ':' '{print $2}' 1.txt b b1 b2 [root@bogon ~]# awk -F ':' '{print $1,$4}' 1.txt a d a1 d1 a2 d2 a_ dddd
匹配第三列含有数字的
awk -F ':' '$3~/[0-9]/' 1.txt
$0 表示整行
~ 模糊匹配
== 精准匹配
匹配第三列含有数字只显示第一列和第三列
awk -F ':' '$3~/[0-9]/ {print $1,$3}' 1.txt
重新指定分割符 OFS要放在前面 打印出的不能是$0
awk -F ':' '$3~/[0-9]/ {OFS="#";print $1,$3}' 1.txt awk -F ':' '$1=="a2"||NR>3 {print $0}' 1.txt
NR 行号
NF 被分割后的列数
||或者 ,&& 且
第一段等于a2或者 行号>3的结果
[root@bogon ~]# awk -F ':' '$3~/[0-9]/' 1.txt a1:b1:c1:d1 a2:b2:c2:d2 [root@bogon ~]# awk -F ':' '$3~/[0-9]/ {print $1,$3}' 1.txt a1 c1 a2 c2 [root@bogon ~]# awk -F ':' '$3~/[0-9]/ {OFS="#";print $1,$3}' 1.txt a1#c1 a2#c2
精准匹配
字符必须加引号
awk -F ':' '$1=="a2" {print $0}' 1.txt
[root@bogon ~]# awk -F ':' '$1=="a2" {print $0}' 1.txt a2:b2:c2:d2 [root@bogon ~]# awk -F ':' '$1=="a2"||NR>3 {print $0}' 1.txt a2:b2:c2:d2 a_: :c:dddd 表达式 [root@bogon ~]# awk -F ':' '$1=$2$3 {print $0}' 1.txt bc b c d b1c1 b1 c1 d1 b2c2 b2 c2 d2 c c dddd
查找分割后 列数 是3到5个的
awk -F ':' 'NF>=3&&NF<=5 {print}' 1.txt
[root@bogon ~]# cat 1.txt a:b:c:d a1:b1:c1:d1 e:f e1:f1:g1 e2:f2:g2:h2:i:j:k [root@bogon ~]# awk -F ':' 'NF>=3&&NF<=5 {print}' 1.txt a:b:c:d a1:b1:c1:d1 e1:f1:g1
awk -F ':' '{OFS="#";print $NR,$NF}' 1.txt
[root@bogon ~]# cat 1.txt a:b:c:d a1:b1:c1:d1 e:f e1:f1:g1 e2:f2:g2:h2:i:j:k [root@bogon ~]# awk -F ':' '{OFS="#";print $NR,$NF}' 1.txt a#d b1#d1 #f #g1 i#k
对于以上 $NR,$NF的解析
1,4
2,4
3,2
4,3
5,7
原文地址:https://www.cnblogs.com/HKUI/p/6446114.html
相关推荐
-
谁偷了1/3个CPU – 诡异Go性能问题追根问底 服务器
2020-6-22
-
Docker从安装部署到Hello World 服务器
2019-10-7
-
centos 6.4 mysql rpm 离线安装【备忘】 服务器
2019-10-5
-
基于Tensorflow的验证码识别 服务器
2020-6-22
-
Kafka架构设计简介 服务器
2020-6-8
-
如何使用Rancher 2.0在Kubernetes集群上部署Istio 服务器
2020-6-16
-
使用 Docker 容器应该避免的 10 个事情 服务器
2019-3-10
-
跑步进入全站 HTTPS ,这些经验值得你看看 服务器
2019-3-10
-
ElasticSearch2.3.3离线搭建(windows+Linux) 服务器
2020-5-31
-
使用Jenkins Pipeline插件和Docker打造容器化构建环境 服务器
2020-6-21