1、printf 命令严格来说不是字符截取命令,但是通常和cut命令放在一起用
,作为标准输出;
2、printf 命令:
printf '输出类型输出格式' 输出内容
输出类型:
%ns: 输出字符串。n是数字指代输出几个字符
%ni : 输出整数。n是数字指代输出几个数字
%m.nf : 输出浮点数。m和n是数字,指代输出的整数位数和小数位数。如%8.2f代表共输出8位数,其中2位是小数,6位是整数。
输出格式:
\a :输出警告声音
\b:输出退格键,也就是Backspace键
\f :清除屏幕
\n :换行
\r :回车,也就是Enter键
\t :水平输出退格键,也就是Tab键
\v :垂直输出退格键,也就是Tab键
[root@localhost sh]# printf %s 1 2 3 4 5 6
123456[root@localhost sh]# printf %s %s %s 1 2 3 4 5 6
%s%s123456[root@localhost sh]# printf '%s %s %s' 1 2 3 4 5 6
1 2 34 5 6[root@localhost sh]# printf '%s %s %s \n' 1 2 3 4 5 6
1 2 3
4 5 6
#如下,'%s %s %s\n'表示将内容按照后面的内容按照%s的个数为一组,然后换行,在此即表示三个一组,然后换行
[root@localhost sh]# printf 1 2 3 4 5 6
1 2 3
4 5 6
[root@localhost sh]#
[root@localhost sh]# printf '%s' student.txt
student.txt[root@localhost sh]# printf '%s' $(cat student.txt )
IDNamegenderMark1LimingM862ScM903GaoM83[root@localhost sh]#
学习printf命令的原因是:
awk编程中无法直接调用系统中的cat 等命令,内容输出需要用到printf 协助,所以在此学习
如上:
》printf '%s' $(cat student.txt)
#不调整输出格式
》printf '%s\t %s\t %s\t %s\t %s\t %s\t \n' $(cat student.txt)
#调整格式输出
[root@localhost sh]# cat student.txt
ID Name gender Mark
1 Liming M 86
2 Sc M 90
3 Gao M 83
[root@localhost sh]# printf '%s\t %s\t %s\t %s\t %s\t %s\t \n' $(cat student.txt)
ID Name gender Mark 1 Liming
M 86 2 Sc M 90
3 Gao M 83
3、在awk明星的输出中支持print 和printf命令
》print:print会在每个输出之后自动加入一个换行符(Linux默认没有print命令)
》printf:printf是标准格式输出命令,并不会自动加入换行符,如果需要换行,需要手工加入换行符;