zoukankan      html  css  js  c++  java
  • Linux上printf命令的用法

    printf格式化输出

    基本格式

    printf [format] [文本1] [文本2] ..

    常用格式替换符

    %s
    字符串
    %f
    浮点格式
    %c
    ASCII字符,即显示对应参数的第一个字符
    %d,%i
    十进制整数
    %o
    八进制值
    %u
    不带正负号的十进制值
    %x
    十六进制值(a-f)
    %X
    十六进制值(A-F)
    %%
    表示%本身

    常用转义字符

    a
    警告字符,通常为ASCII的BEL字符
    
    后退
    f
    换页
    换行
    回车
    水平制表符
    v
    垂直制表符
    \
    表示本身

    使用示例

    [keysystem@localhost ~]$ printf "%s
    " 1 2 3 4
    1
    2
    3
    4
    [keysystem@localhost ~]$ printf "%f
    " 1 2 3 4
    1.000000
    2.000000
    3.000000
    4.000000
    [keysystem@localhost ~]$ printf "%.2f
    " 1 2 3 4
    1.00
    2.00
    3.00
    4.00
    [keysystem@localhost ~]$ printf " (%s) " 1 2 3 4;echo ""
     (1)  (2)  (3)  (4) 
    [keysystem@localhost ~]$ printf "%s %s
    " 1 2 3 4
    1 2
    3 4
    [keysystem@localhost ~]$ printf "%s %s %s
    " 1 2 3 4
    1 2 3
    4  
    [keysystem@localhost ~]$ 
    
    [keysystem@localhost ~]$ #"-"表示左对齐, "10 10 4 4" 表示占的字符位数, 不够不空格
    [keysystem@localhost ~]$ printf "%-10s %-10s %-4s %-4s 
    " 姓名 性别 年龄 体重 苹果 男 18 60 香蕉 男 18 80
    姓名     性别     年龄 体重 
    苹果     男        18   60   
    香蕉     男        18   80   
    [keysystem@localhost ~]$ printf "%X" 13    #10进制转16进制
    D[keysystem@localhost ~]$ printf "%X
    " 13    
    D
    [keysystem@localhost ~]$ printf "%d" 0xB    #16进制转10进制
    11
  • 相关阅读:
    python 中: lambda
    python 学习 argparse
    深度学习 ——style reconstruction
    简单linux命令1
    intptr_t 指针
    MySQL数据库基本命令-1
    交换机和路由器的区别
    UML图的使用
    操作系统总结链接
    操作系统总结
  • 原文地址:https://www.cnblogs.com/alsodzy/p/8404305.html
Copyright © 2011-2022 走看看