Linux - formatted output printf command

Printf format output command

Command syntax: 
-printf 'output type and format' outputting content 

output type: 
-% NS output string, n being a number, representing the output of several characters 
-% ni output an integer, n being a number, several numbers representing the output 
-% m .nf output floating, m and n are numbers, it refers to the number of integer bits and scale generation of output. The% 8.2f represents an integer of 6 bits, two decimal places. 

Output Format: 
- \ A: outputting a warning sound 
- \ b: Output backspace key is the Backspace 
- \ f: apparent screen 
- \ n: newline 
- \ r: the transport, i.e. the Enter 
- \ T: Tab , Tab key 
- \ v: vertical output backspace key, the Tab key is
Example: operation output printf% s 1 2 3 4 5 6
[root@192 cut]# printf %s 1 2 3 4 5 6
123456[root@192 cut]#
Example: operation output printf% s% s% s 1 2 3 4 5 6
[root@192 cut]# printf %s %s %s 1 2 3 4 5 6
%s%s123456[root@192 cut]#
This result is not what we want.
Example: operation output printf '% s% s% s' 1 2 3 4 5 6
[root@192 cut]# printf '%s %s %s' 1 2 3 4 5 6
1 2 34 5 6[root@192 cut]#
This time, a little way, but still not what we want
Example: operation output printf '% s% s% s \ n' 1 2 3 4 5 6
[root@192 cut]# printf '%s %s %s\n' 1 2 3 4 5 6
1 2 3
4 5 6
[root@192 cut]#
This is the result we want!
Example: Use of printf score.txt file printf '% s' student.txt
[root@192 cut]# printf '%s' score.txt
score.txt[root@192 cut]#
This result is not what we want, we ought output is the content.
Example: Using the text of printf output score.txt
[root@192 cut]# printf '%s' $(cat score.txt)
IdNameGenderScore1zhangsanM902lisiM883wangwuM984zhaoliuN975NangongYiM100[root@192 cut]#
Note : The output text, you will need to get the text, and then assigned to a variable, so, printf can be manipulated.
Example: Formatting is very beautiful drop text output score.txt
[root@192 cut]# printf '%s\t%s\t%s\t\n' $(cat score.txt)
[root@192 cut]# printf '%s\t%s\t%s\t%s\t\n' $(cat score.txt)
Id	Name	Gender	Score
1	zhangsan	M	90
2	lisi	M	88
3	wangwu	M	98
4	zhaoliu	N	97
5	NangongYi	M	100
[root@192 cut]#
In the awk command's output, support print and printf command 
1, print: print after each output will automatically add a newline (Linux default no print command)
2, printf: printf format is the standard output of the command, and will not manually add line breaks, line breaks if necessary, require manual addition of line breaks
3, Linux system, there is no print command.
Published 59 original articles · won praise 2 · Views 5568

Guess you like

Origin blog.csdn.net/LDR1109/article/details/102958400