微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

shell字符截取命令之awk命令

一 语法


二 实战
[root@localhost ~]# cut -f 2,4 student.txt
furong 85
fengj 60
cang 70
[root@localhost ~]# awk '{printf $2 "\t" $4 "\n"}' student.txt
furong 85
fengj 60
cang 70
[root@localhost ~]# awk '{print $2 "\t" $4}' student.txt
furong 85
fengj 60
cang 70
[root@localhost ~]# df -h|awk '{print $1 "\t" $3}'
Filesystem Used
/dev/sda3 124M
devtmpfs 0
tmpfs 84K
tmpfs 7.0M
tmpfs 0
/dev/sda5 3.9G
/dev/sda7 33M
/dev/sda2 1.3G
/dev/sda1 153M
tmpfs 16K
tmpfs 0
三 在awk命令的输出支持print和printf命令


四 实战
[root@localhost ~]# df -h|grep "/dev/sda5" |awk '{print $5}'
39%
[root@localhost ~]# df -h|grep "/dev/sda5" |awk '{print $5}'|cut -d "%" -f 1
39
[root@localhost ~]# awk 'BEGIN{printf "This is a transcript \n"}{printf $2 "\t" $4 "\n"}' student.txt
This is a transcript
furong 85
fengj 60
cang 70
[root@localhost ~]# awk 'END{printf "This is a transcript \n"}{printf $2 "\t" $4 "\n"}' student.txt
furong 85
fengj 60
cang 70
This is a transcript
[root@localhost ~]# cat /etc/passwd | grep "/bin/bash"
root:x:0:0:root:/root:/bin/bash
cakin24:x:1000:1000:cakin24,cakin,cakin:/home/cakin24:/bin/bash
test:x:1003:1001::/home/xxx:/bin/bash
cls:x:1001:1001:dgdzmx:/home/cls:/bin/bash
[root@localhost ~]# cat /etc/passwd | grep "/bin/bash"|\
> awk 'BEGIN{FS=":"}{printf $1 "\t" $3 "\n"}'
root 0
cakin24 1000
test 1003
cls 1001
[root@localhost ~]# cat student.txt |awk '$4>=70{printf $2 "\n"}'
furong
cang

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐