Reprint: Use of awk printf in the shell

Source: Internet
Author: User
Tags echo command modifier modifiers

Reprint: http://www.linuxawk.com/jiaocheng/83.html

6. printf functions

When you print the output, you may need to specify the number of spaces between the fields to arrange the columns neatly. Using tabs in the Print function does not guarantee the desired output, so you can use the printf function to format the particular output.

The printf function returns a formatted string to the standard output, just like a printf statement in the C language. The printf statement includes a quoted control string, which may have several format descriptions and modifiers embedded in the control string. The control string is followed by a comma followed by a comma-delimited list of expressions. The printf function orchestrates the format of these expressions according to the instructions in the control string. Unlike the print function, printf does not wrap lines at the end of a line. Therefore, if you want to wrap, you must provide the escape character \ n in the control string.

Each percent semicolon and format description must have a corresponding variable. To print a percent sign, you must give two percent semicolons in the control string. Please refer to the print escape character and the printf modifier. The format description is presented by a percent semicolon, and the format specifier used by printf is also listed.

Escape characters used by printf

Escape Character definition
C Character
S String
D Decimal integer
Ld Decimal Long Integer
U Decimal unsigned integer
Lu Decimal unsigned long integer
X hexadecimal integer
Lx Hexadecimal Long integer
O Eight-binary integers
Lo octal long integers
E Floating-point number represented by scientific notation (e notation)
F Floating point number
G Choose a shorter form of E or F

Modifiers for printf

character definition
- Left alignment modifier
# When displaying 8 binary integers, add a 0 in front
Display 16 binary integers in front plus 0x
+ Displays integers converted using D, E, F, and G, plus the sign + or-
0 Fills the displayed values with 0 instead of blanks

Format specifier for printf

format specifier function
%c Print a single ASCII character
printf ("The character is%c\n", X)
Output: The character is A
%d Print a decimal number
printf ("The boy is%d years old\n", y)
Output: The boy was years old
%e The e-notation form of printed numbers
printf ("Z is%e\n", z) Printing: Z is 2.3e+0 1
%f Print a floating-point number
printf ("Z is%f\n", 2.3 * 2)
Output: Z is 4.600000
%o octal for printing numbers
printf ("Y is%o\n", y)
Output: Z is 17
%s Print a string
Print ("The name of the culprit is%s\n", $)
Output: The name of the culprit is Bob Smith
%x Print the hexadecimal value of a number
printf ("Y is%x\n", y)
Output: X is F

When you print a variable, the location of the output is called a field, and the width of the field is the number of characters contained in the field. In these examples, the pipe character (the vertical bar) in the printf control string is part of the text that indicates the start and end of the format.
Example

$echo "Linux" | awk ' {printf "|%-15s|\n", $ '} '
| Linux |

Description: for the output of the echo command, Linux is piped to awk. The printf function contains a control string. The percent semicolon prepares printf to print a 15-cell, left-aligned string that is sandwiched between two vertical bars and ends with a newline character. The dash after the percent semicolon indicates left alignment. The control string is followed by a comma and $. printf formats the string Linux according to the formatting instructions in the control string.

Example

$echo "Linux" | awk ' {printf "|%15s|\n", $ '} '
| Linux|

Description: The string Linux is printed as a 15-cell, right-aligned string, sandwiched between two vertical bars to
End of line break.

Example

$Cat Employees
Tom Jones 4424 5/ 543354
Mary Adams 5346 /4/ 28765
Sally Chang 1654 7/ 650000
Billy Black 1683 9/ 336500 $ awk' {printf ' the name is:%-15s ID is%8d\n ", $1,$3} ' employees
The name is Tom ID is 4424
The name is Mary ID is 5346
The name is Sally ID is 1654
The name is Billy ID is 1683

Description: The string to be printed is placed between two double quotation marks. The first format specifier is%-15s, which corresponds to the comma next to the right half of the control string. The percent-Semicolon presentation format Description: The dash represents left-aligned, and 15s represents a 15-cell string. This command prints a left-aligned, 15-cell string followed by the ID of the string and an integer.

Format:%8d indicates that a decimal (integer) value is printed in this position of the string. This integer occupies 8 cells and is aligned to the right. You can also choose to enclose the quoted string and expression in parentheses.

Reprint: Use of awk printf in the shell

Related Article

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.