Shell编程-Cut命令-printf命令-awk命令

  • awk内置变量
  • 图片来自:https://www.cnblogs.com/jiqianqian/p/7944013.htmlShell编程-Cut命令-printf命令-awk命令_第1张图片
[root@izwz97473w2ydu1pgsmzk4z test]# cut -f 2 student.txt 
NAME
1    furong  F        56
2    fengjie F        76
3    cang    F        99
[root@izwz97473w2ydu1pgsmzk4z test]# vim student.txt 
[root@izwz97473w2ydu1pgsmzk4z test]# cut -f 2 student.txt 
NAME
furong
fengjie
cang

  • 局限性:df -h搜索不起作用。因为df- h下面的表格不是以空格作为分隔符
  • 注意小数点
    Shell编程-Cut命令-printf命令-awk命令_第2张图片
    Shell编程-Cut命令-printf命令-awk命令_第3张图片
    Shell编程-Cut命令-printf命令-awk命令_第4张图片
[root@izwz97473w2ydu1pgsmzk4z test]# printf %s %s %s 123 4 5 6
%s%s123456[root@izwz97473w2ydu1pgsmzk4z test]# printf '%s' $(cat student.txt)
IDNAMEGENDERMARK1furongF562fengjieF763cangF99[root@izwz97473w2ydu1pgsmzk4z test]# 
[root@izwz97473w2ydu1pgsmzk4z test]# printf '%s\t%s\t%s\t\n' $(cat student.txt)
ID	NAME	GENDER	
MARK	1	furong	
F	56	2	
fengjie	F	76	
3	cang	F	
99			
[root@izwz97473w2ydu1pgsmzk4z test]# printf '%s\t%s\t%s\t%s\n' $(cat student.txt)
ID	NAME	GENDER	MARK
1	furong	F	56
2	fengjie	F	76
3	cang	F	99

Shell编程-Cut命令-printf命令-awk命令_第5张图片
Shell编程-Cut命令-printf命令-awk命令_第6张图片

  • 关于awk和printf语句
  • 因为单引号被awk使用,所以你在printf里面再写单引号会导致混淆,所以,里面的printf语句不使用单引号,但是,转义符号必须使用双引号括起来,不然也是无效的
  • 不加任何条件时候,无条件执行
[root@izwz97473w2ydu1pgsmzk4z test]# awk '{printf $1 "\t" $4 "\n"}' student.txt 
ID	MARK
1	56
2	76
3	99
[root@izwz97473w2ydu1pgsmzk4z test]# 

Shell编程-Cut命令-printf命令-awk命令_第7张图片

[root@izwz97473w2ydu1pgsmzk4z test]# df -h | grep "^/dev$"
[root@izwz97473w2ydu1pgsmzk4z test]# df -h | grep "/dev$"
devtmpfs        910M     0  910M   0% /dev
[root@izwz97473w2ydu1pgsmzk4z test]# df -h | grep "/dev$" | awk 'printf{ $4 "\n"}'
awk: cmd. line:1: printf{ $4 "\n"}
awk: cmd. line:1: ^ syntax error
[root@izwz97473w2ydu1pgsmzk4z test]# df -h | grep "/dev$" | awk '{printf $4"\n"}'
910M
[root@izwz97473w2ydu1pgsmzk4z test]# df -h | grep "/dev$" | awk '{printf $4"\n"}'|cut -d 'M'
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.
[root@izwz97473w2ydu1pgsmzk4z test]# df -h | grep "/dev$" | awk '{printf $4"\n"}'|cut -d 'M' -f 1
910
[root@izwz97473w2ydu1pgsmzk4z test]# 

Shell编程-Cut命令-printf命令-awk命令_第8张图片

  • 先读取一行数据,在从中按照条件来截取

Shell编程-Cut命令-printf命令-awk命令_第9张图片
Shell编程-Cut命令-printf命令-awk命令_第10张图片

[root@izwz97473w2ydu1pgsmzk4z test]# cat /etc/passwd |grep /bin/bash |awk '{FS=":"}{printf $1 "\t" $4 "\n"}'
root:x:0:0:root:/root:/bin/bash	
admin	1000
[root@izwz97473w2ydu1pgsmzk4z test]# cat /etc/passwd |grep /bin/bash |awk 'BEGIN{FS=":"}{printf $1 "\t" $4 "\n"}'
root	0
admin	1000
[root@izwz97473w2ydu1pgsmzk4z test]# awk '{print $1 "\t" $4}' student.txt 
ID	MARK
1	56
2	76
3	99
[root@izwz97473w2ydu1pgsmzk4z test]# awk '{print $1 "\t" $4}' student.txt 
student.txt  zheng.txt    
[root@izwz97473w2ydu1pgsmzk4z test]# awk 'BEGIN{print  i am sb}{print $1 "\t" $4}' student.txt 

ID	MARK
1	56
2	76
3	99
[root@izwz97473w2ydu1pgsmzk4z test]# awk 'BEGIN{print "i am sb"}{print $1 "\t" $4}' student.txt 
i am sb
ID	MARK
1	56
2	76
3	99
[root@izwz97473w2ydu1pgsmzk4z test]# awk 'END{print "i am sb"}{print $1 "\t" $4}' student.txt 
ID	MARK
1	56
2	76
3	99
i am sb

Shell编程-Cut命令-printf命令-awk命令_第11张图片
Shell编程-Cut命令-printf命令-awk命令_第12张图片

你可能感兴趣的:(shell)