您的位置:首页 > 其它

格式化输出-printf命令

2016-07-04 18:25 323 查看
格式化输出命令

printf 和 echo命令类似,输出

printf 是linux 的标准输出命令

#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 ~]# printf %8.2f 11111.34
11111.34[root@localhost ~]# printf %s 1 2 3 4 5 6
123456[root@localhost ~]# echo 1 2 3 4 5 6
1 2 3 4 5 6
[root@localhost ~]# printf %s %s %s  1 2 3 4 5 6
%s%s123456[root@localhost ~]# printf '%s %s %s'  1 2 3 4 5 6
1 2 34 5 6[root@localhost ~]# printf '%s\t %s\t %s'  1 2 3 4 5 6
1    2   34  5   6[root@localhost ~]# printf '%s\t %s\t %s\n'  1 2 3 4 5 6
1    2   3
4    5   6
[root@localhost ~]# printf '%s\t %s\t %s\n'  1 2 3 4 5 6
1    2   3
4    5   6


文件中的内容:

[root@localhost home]# cat student.txt
ID  Name    gender  Mark
1   furong  F   88
2   fengjie F   60
3   cang    F   70


读取文件中的内容:

[root@localhost home]# printf '%s' $(cat student.txt)
IDNamegenderMark1furongF882fengjieF603cangF70[root@localhost home]#
[root@localhost home]# printf '%s\t%s\t%s\t%s\n' $(cat student.txt)
ID  Name    gender  Mark
1   furong  F   88
2   fengjie F   60
3   cang    F   70


注意:

在awk命令的输出中支持print和printf命令

print:print会在每个输出之后自动添加一个换行符(Linux默认没有print命令)

printf:printf是标准格式输出命令,并不会自动添加换行符,如果需要换行,需要手工添加换行符
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  printf