Linux awk sprintf() Function: Text Formatting Tutorial

The sprintf function in the awk allows you to format strings in a customizable way.

In this tutorial, we’ll learn various aspects of using sprintf, from basic syntax to advanced formatting methods.

Whether you’re formatting dates, numbers, or complex strings, sprintf offers the flexibility you need to handle any text formatting.

 

 

Syntax and Usage

The basic syntax is:

awk '{ print sprintf("format", variables) }' file
  • { print sprintf("format", variables) }  You provide a format string followed by one or more variables.
  • file is the input file containing the data you want to process.

Suppose you have a dataset like this:

1023 583.23
1145 960.50
1289 204.17

You can format the data using awk sprintf:

awk '{ print sprintf("User ID: %d, Data Used: %.2f MB", $1, $2) }' data.txt

Output:

User ID: 1023, Data Used: 583.23 MB
User ID: 1145, Data Used: 960.50 MB
User ID: 1289, Data Used: 204.17 MB

This command displays each user’s ID and data usage in a more readable format.

%d is used for decimal (integer) formatting of user IDs, and %.2f formats the data usage to two decimal places.

 

Format Specifiers in sprintf

The sprintf specifiers control how a value should be formatted.

Here is a table for common format specifiers:

Specifier Description
%d Decimal integer
%f Floating-point number
%s String
%c Single character
%o Octal number
%x Hexadecimal number (lowercase)
%X Hexadecimal number (uppercase)
%e Scientific notation (lowercase)
%E Scientific notation (uppercase)
%g Use %e or %f, whichever is shorter
%G Use %E or %f, whichever is shorter
%u Unsigned decimal integer
%% A literal ‘%’ character
%p Pointer (address in hexadecimal)

 

Format String

Truncate and Padding Strings

Command:

echo "UserID: A123456" | awk '{ print sprintf("ID: %-.4s", $2); }'

Output:

ID: A123

This truncates the user ID to the first four characters.

Concatenate Formatted Strings

Command:

echo "2024-04-16 Event" | awk '{ split($1, date, "-"); print sprintf("Day: %s, Month: %s, Year: %s, Description: %s", date[3], date[2], date[1], $2); }'

Output:

Day: 16, Month: 04, Year: 2024, Description: Event

This example shows how sprintf can be used to concatenate and format multiple strings.

 

Format Numbers

Format Integers

You can format integers using sprintf %d specifier:

echo "54321" | awk '{ printf "Original Number: %s\n", $0; formatted=sprintf("%08d", $1); print "Formatted Number: " formatted; }'

Output:

Original Number: 54321
Formatted Number: 00054321

Here, %08d pads the integer with zeros to ensure an 8-digit representation.

Format Floating-Point Numbers

You can format integers using sprintf %f specifier:

echo "123.456" | awk '{ printf "Original Number: %f\n", $0; formatted=sprintf("%.2f", $1); print "Formatted Number: " formatted; }'

Output:

Original Number: 123.456000
Formatted Number: 123.46

The %.2f specifier rounds the floating-point number to two decimal places.

Specify Precision for Numbers

You can specify the precision for numbers in sprintf by specifying the number of decimal places in the %f specifier:

echo "12345.6789" | awk '{ formatted=sprintf("%.3f", $1); print "Formatted Number: " formatted; }'

Output:

Formatted Number: 12345.679

The %.3f specifier rounds the number to three decimal places.

Scientific Notation and Exponents

You can use sprintf %e specifier to convert the number to scientific notation:

echo "12345.6789" | awk '{ formatted=sprintf("%e", $1); print "Formatted Number: " formatted; }'

Output:

Formatted Number: 1.234568e+04

Format Hexadecimal and Octal Numbers

You can format hexadecimal numbers using %x specifier and %X for uppercase, and for octal numbers, you can use %o specifier.

echo "255" | awk '{ printf "Decimal: %d, Hexadecimal: %x, Octal: %o\n", $1, $1, $1; }'

Output:

Decimal: 255, Hexadecimal: ff, Octal: 377

 

Format Date and Time

Command:

echo | awk '{ formatted=sprintf("Current Date and Time: %s", strftime("%Y-%m-%d %H:%M:%S")); print formatted; }'

Output:

Current Date and Time: 2024-04-16 10:30:45

Here, strftime("%Y-%m-%d %H:%M:%S") is used with sprintf to format the current date and time in a standard format.

Custom Date and Time Format

Command:

echo | awk '{ formatted=sprintf("Formatted Date: %s", strftime("%d/%m/%Y")); print formatted; }'

Output:

Formatted Date: 16/04/2024

 

Multiple sprintf Calls

Multiple sprintf calls in awk  allow you to build complex formatted strings in a step-by-step manner.

echo "12345.678" | awk '{ 
    formatted_inner = sprintf("%.2f", $1); 
    formatted_outer = sprintf("Formatted to two decimal places: %s", formatted_inner); 
    print formatted_outer; 
}'

Output:

Formatted to two decimal places: 12345.68

In this example, the inner sprintf call formats the number to two decimal places.

The outer sprintf then takes this formatted string and embeds it into a more descriptive sentence.

 

Format Output

You can use escape characters such as \n (new line) and \r (carriage return) to control the layout of the output:

echo "Data1,Data2" | awk -F, '{ formatted=sprintf("First Data: %s\nSecond Data: %s", $1, $2); print formatted; }'

Output:

First Data: Data1
Second Data: Data2

In this script, \n is used within sprintf to insert a newline between two pieces of data.

Leave a Reply

Your email address will not be published. Required fields are marked *