Summarize the usage of the printf command under Linux

Source: Internet
Author: User
Tags binary to decimal ibase

printf--Format and print date formatted output data with printf options


Reference: http://www.computerhope.com/unix/uprintf.htm


Basic English Learning


Binary: Binanry number

Octal: otcal number

Decimal: Decimal number

Hex: Hexadecimal number

1 Basic printout (default non-line break \ n NewLine)

[[email protected] gold]# printf "Hello,world"

Hello,world[[email protected] gold]#

[[email protected] gold] #printf "hello,world\n"

Hello,world


Conversion of 2 binary

%x Hex

%d decimal

%o Octal


Decimal Turn 16 binary

[[email protected] gold]# printf "%x\n" 27

1b


16 Binary to Decimal

[[email protected] gold]# printf "%d\n" 0x1b

27


(Note that the conversion can be made using the BC command, such as 2 binary) ibase input, obase output of the binary)

10 Binary to 2 binary

[Email protected] gold]# echo ' obase=2;10 ' |BC

1010

Handling with variables is commonly used in shell scripts

[Email protected] tmp]# a=10

[Email protected] tmp]# echo "obase=2; $a" |BC

1010

2 Binary Conversion 10 binary

[Email protected] gold]# echo ' ibase=2;111 ' |BC

7




Advanced usage in general:

[email protected] tmp]# cat file.txt 100200300
[[email protected] tmp]# awk ' {print ' ibase=10;obase=2; ' $ ' file.txt |bc |xargs printf '%08d\n ' 011001001100100010010110 0



3 Formatting output alignment (Chinese and English mixed effect is not obvious)

Syntax: %flags width.precision format-specifier

-Is flag left justified by default right justified

12 is width indicates the length of the character occupies extra space, the length of the string is not enough to display.

. precision represents the decimal place of a floating-point number



650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/8C/9C/wKiom1hxvv_CrtPqAAATyPjtbYk958.png "title=" Print1.png "alt=" Wkiom1hxvv_crtpqaaatypjtbyk958.png "/>






This article is from "Swallow Lie triple" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1890139

Summarize the usage of the printf command under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.