字符截取命令-awk命令
来源:互联网 发布:淘宝客手机版 编辑:程序博客网 时间:2024/06/06 03:48
awk命令是一个异常复杂的命令。了解常用的用法。
功能:文件中截取我们需要的数据
#awk '条件1{动作1}条件2{动作2}...' 文件名
说明:
条件(Pattern)
- 一般使用关系表达式作为条件
- x>10判断变量 x是否大于10
- x>=10 大于等于10
- x<=10 小于等于10
动作(Action)
- 格式化输出
- 流程控制语句
例如:
[root@localhost home]# cat student.txt ID Name gender Mark1 furong F 882 fengjie F 603 cang F 70
查看文件student.txt第二列和第四列
[root@localhost home]# awk '{printf $2 "\t" $4 "\n"}' student.txt Name Markfurong 88fengjie 60cang 70
说明
- awk一行一行的的读取文件
- 判断条件,如果条件满足,则执行动作
- 把文件名赋值给$0字段,将ID赋值给$1字段,依次类推
- awk默认是空格或者制表符做为分隔符
- 没有条件限制就是无条件显示执行动作
- 仅仅是提取制表符和有规律的
示例:
[root@localhost home]# df -h | awk '{print $5}'Use%21%0%30%1%1%[root@localhost home]# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda6 5.9G 1.2G 4.5G 21% /tmpfs 491M 0 491M 0% /dev/shm/dev/sda1 190M 54M 127M 30% /boot/dev/sda2 9.5G 23M 9.0G 1% /home/dev/sda5 1.9G 3.1M 1.8G 1% /usr/local[root@localhost home]# df -h | grep "/dev/sda6" | awk '{print $%}'awk: {print $%}awk: ^ syntax error[root@localhost home]# df -h | grep "/dev/sda6" | awk '{print $5}'21%[root@localhost home]# df -h | grep "/dev/sda6" | awk '{print $5}' | cut -d "%" -f 121
示例2
[root@localhost home]# cat student.txt ID Name gender Mark1 furong F 882 fengjie F 603 cang F 70[root@localhost home]# awk 'BEGIN{print "this is a test"}{print $2 "\t" $4}' student.txt this is a testName Markfurong 88fengjie 60cang 70[root@localhost home]# awk 'END{print "this is a test"}{print $2 "\t" $4}' student.txt Name Markfurong 88fengjie 60cang 70this is a test
BEGIN作用,awk默认是用空格或者制表符进行分割的。BEGIN作用是设定变量来做分隔符。如果不写BEGIN,则awk会从读取第一行后才能执行按照分隔符分割的。
[root@localhost home]# cat /etc/passwd |grep "/bin/bash"root:x:0:0:root:/root:/bin/bashimooc:x:502:889:kk,kk,kk,kk:/home/xxx:/bin/bashcls:x:500:889:dgdzmx:/home/cls:/bin/bashuser1:x:503:503::/home/user1:/bin/bashtony:x:504:504::/home/tony:/bin/bashlw:x:505:505::/home/lw:/bin/bashuser2:x:506:506::/home/user2:/bin/bash[root@localhost home]# cat /etc/passwd |grep "/bin/bash" | awk 'BEGIN{FS=":"}{printf $1 "\t" $3}'root 0imooc 502cls 500user1 503tony 504lw 505user2 506[root@localhost home]# cat /etc/passwd |grep "/bin/bash" | awk 'BEGIN{FS=":"}{printf $1 "\t" $3 "\n"}'root 0imooc 502cls 500user1 503tony 504lw 505user2 506[root@localhost home]# cat /etc/passwd |grep "/bin/bash" |grep -v "root" | awk 'BEGIN{FS=":"}{printf $1 "\t" $3 "\n"}'imooc 502cls 500user1 503tony 504lw 505user2 506[root@localhost home]#
0 0
- 字符截取命令-awk命令
- Shell编程-字符截取命令-awk命令
- shell字符截取命令之awk命令
- [Shell]字符截取命令:cut, printf, awk, sed
- Linux 字符截取命令 cut printf awk sed
- Shell 字符截取命令:cut, printf, awk, sed
- linux 下 awk 命令截取
- Linux 字符截取命令
- linuxshell编程之字符截取命令cut、awk、printf、sed、sort
- shell-字符截取命令sed
- Linux字符截取命令-cut
- Linux之字符截取命令
- shell编程-字符截取命令
- Shell编程-字符截取命令-cut命令
- Shell编程-字符截取命令-printf命令
- Shell编程-字符截取命令-sed命令
- shell字符截取命令之cut命令
- 批处理命令set截取字符详解
- DTD语法
- ZFS文件系统
- flash builder概要分析器使用
- 深入理解android UI Thread
- artTemplate.js 实例
- 字符截取命令-awk命令
- 现代OpenGL教程 01 - 入门指南
- DB2和 Oracle的并发控制(锁)比较
- Spring boot 下Freemarker的注解方式配置
- Struts2中过滤器和拦截器的区别
- Spring的优点,方便记忆版
- couldn’t be opened because you don’t have permission to view it.
- C++经典书目
- 设计模式——命令模式实现undo、redo