copied to clipboard!
string awk

Mastering Data Formatting with awk and printf: A Beginner’s Guide

updated: 2026/05/05 created: 2026/04/27

Introduction

awk is a powerful command specialized for text processing, frequently used in log analysis and data formatting tasks.

Among its features, printf is an important function that allows fine control over output formatting.
Beginners often struggle with the differences from print and the concept of format specifiers, but once understood, it greatly improves work efficiency.

This article explains the topic in a practical way while touching on the points where beginners tend to stumble.

Reference: GNU gawk

The Basic Role of printf in awk and the Difference from print

Create File

cat << 'EOF' > input.txt apple 100 banana 200 orange 150 EOF

Command

awk '{print $1, $2}' input.txt

Output

apple 100
banana 200
orange 150

Command

awk '{printf "%s : %d\n", $1, $2}' input.txt

Output

apple : 100
banana : 200
orange : 150

Command

awk '{printf "%-10s %5d\n", $1, $2}' input.txt

Output

apple          100
banana         200
orange         150

How It Works

Itemprintprintf
NewlineAdded automaticallyNot added automatically (\n required)
Format specifierNot available (simple output)Available (%s, %d, width, etc.)
Output flexibilityLowHigh (alignment, padding, etc.)
Use caseSimple outputWhen formatted output is needed

Explanation

print is suited for simple output, while printf allows fine-grained format control similar to C language.
Use printf when formatted output is required.

Basic Syntax of printf and How to Use Format Specifiers (%s, %d, %f)

Create File

cat << 'EOF' > input.txt Alice 25 165.5 Bob 30 172.3 Charlie 22 180.0 EOF

Command

awk '{ printf("Name: %s | Age: %d | Height: %.1f\n", $1, $2, $3) }' input.txt

Output

Name: Alice | Age: 25 | Height: 165.5
Name: Bob | Age: 30 | Height: 172.3
Name: Charlie | Age: 22 | Height: 180.0

How It Works

ElementDescriptionExample
printfFunction for formatted outputprintf("...")
%sOutputs a stringName (Alice)
%dOutputs an integerAge (25)
%fOutputs a floating-point numberHeight (165.5)
%.1fDisplays one decimal place165.5
$1, $2, $3Field references$1=Name

Explanation

awk printf allows formatted output with fine control over digits and display style.
In particular, %.nf lets you flexibly adjust the number of decimal places displayed.

Column Alignment (Left/Right Justification) and Padding Settings

Create File

cat << 'EOF' > input.txt apple 50 banana 7 cherry 123 EOF

Command

awk '{ printf "%-10s %5d\n", $1, $2 }' input.txt

Output

apple          50
banana          7
cherry        123

Command

awk '{ printf "%10s %05d\n", $1, $2 }' input.txt

Output

     apple 00050
    banana 00007
    cherry 00123

How It Works

FormatMeaningExample (apple)Description
%-10sLeft-justified, 10-character widthapple_____Pads with spaces on the right
%10sRight-justified, 10-character width_____applePads with spaces on the left
%5dRight-justified, 5-digit width (number)___50Right-justifies the number
%05dZero-padded, 5-digit width (number)00050Pads leading digits with zeros

Explanation

Using printf in awk allows you to align strings and numbers by column width.
By specifying - or 0, you can flexibly control left-justification and zero-padding.

Controlling Decimal Places and Rounding

Create File

cat << 'EOF' > input.txt 3.14159 2.71828 1.23456 EOF

Command

awk '{ printf("%.2f\n", $1) }' input.txt

Output

3.14
2.72
1.23

Command

awk '{ printf("%.3f\n", $1) }' input.txt

Output

3.142
2.718
1.235

How It Works

ElementDescriptionNotes
awkText processing toolProcesses data line by line
$1Field 1The input number
printfFormatted outputC-style format
%.2f2 decimal placesRounds and displays
%.3f3 decimal placesRounds to specified digits

Explanation

The printf format specifier (e.g., %.2f) controls the number of decimal places and automatically rounds the value.
Since awk processes each line, it is well-suited for formatting multiple data values.

Using Escape Sequences (Controlling Newlines, Tabs, and Special Characters)

Create File

cat << 'EOF' > input.txt Hello\tWorld Line1\nLine2 Price: \$100 EOF

Command

awk '{ gsub(/\\t/,"\t"); gsub(/\\n/,"\n"); gsub(/\\\$/,"$"); printf "%s\n", $0 }' input.txt

Output

Hello	World
Line1
Line2
Price: $100

How It Works

ElementDescription
gsubFunction that performs string substitution
/\\t/Detects the literal string "\t"
"\t"Converts to an actual tab character
/\\n/Detects the literal string "\n"
"\n"Converts to an actual newline
/\\\$/Detects "$"
"$"Outputs as $
printf "%s\n"Outputs formatted as-is

Explanation

Since BSD awk does not support %b, gsub is used to individually expand escape sequences.
This enables the same output without depending on GNU awk.

Flexible Output Layout Using Variables

Create File

cat << 'EOF' > input.txt Alice 80 Bob 95 Charlie 70 EOF

Command

awk '{ printf "Name: %-10s | Score: %3d\n", $1, $2 }' input.txt

Output

Name: Alice      | Score:  80
Name: Bob        | Score:  95
Name: Charlie    | Score:  70

How It Works

ElementDescription
$1, $2Retrieves the 1st and 2nd column fields
printfOutputs formatted and aligned text
%-10sLeft-justified string with 10-character width
%3dRight-justified integer with 3-digit width
\nNewline

Explanation

Using printf in awk allows fine control over column width and alignment.
This makes it possible to produce readable, table-like output.

Converting CSV Data to Report Format

Create File

cat << 'EOF' > input.txt name,age,score Alice,23,85 Bob,30,92 Charlie,28,78 EOF

Command

awk -F',' 'NR>1 { printf "Name: %-10s Age: %-3s Score: %-3s\n", $1, $2, $3 }' input.txt

Output

Name: Alice      Age: 23  Score: 85 
Name: Bob        Age: 30  Score: 92 
Name: Charlie    Age: 28  Score: 78 

How It Works

ElementDescription
-F','Sets the field delimiter to comma
NR>1Skips the first line (header)
$1,$2,$3Each CSV column (name, age, score)
printfFormatted output with specified layout
%-10sLeft-justified with 10-character width
%-3sLeft-justified with 3-character width

Explanation

Using printf in awk allows you to convert CSV data into a formatted report layout.
With control over column width and alignment, you can produce clean, readable output.

Preventing Display Misalignment When Using Japanese (Multibyte Characters)

Create File

cat << 'EOF' > input.txt りんご 100 バナナ 200 みかん 300 EOF

Command

awk '{ printf "%-10s %5d\n", $1, $2 }' input.txt

Output

りんご        100
バナナ        200
みかん        300

Command

awk 'BEGIN{ OFS="\t" } { printf "%s\t%d\n", $1, $2 }' input.txt

Output

りんご	100
バナナ	200
みかん	300

How It Works

ElementDescription
awkCommand that processes text line by line and column by column
printfOutputs formatted text with alignment control
%-10sLeft-justifies a string within a 10-character width
%5dRight-justifies a number within a 5-digit width
IssueJapanese characters are multibyte, causing width misalignment
WorkaroundUse tab delimiters or sufficient width to reduce visible misalignment

Explanation

While awk printf is convenient, Japanese characters have inconsistent display widths, which can cause misalignment.
Using tab delimiters or specifying sufficient width helps reduce display issues.

Advanced Formatting Workflows with External Commands (column, sort)

Create File

cat << 'EOF' > input.txt apple 120 3 banana 80 5 orange 150 2 grape 200 4 EOF

Command

awk '{ printf "%-10s %5d %5d\n", $1, $2, $3 }' input.txt | sort -k2 -n

Output

banana        80     5
apple        120     3
orange       150     2
grape        200     4

Command

awk '{ printf "%-10s %5d %5d\n", $1, $2, $3 }' input.txt | sort -k2 -n | column -t

Output

banana  80   5
apple   120  3
orange  150  2
grape   200  4

How It Works

StepCommand ElementRole
1awk printfFormats with specified column width (left/right justification)
2sort -k2 -nSorts ascending by the 2nd column (numeric)
3column -tReformats whitespace-delimited output into a table layout

Explanation

By fixing the format with awk printf, sorting with sort, and improving readability with column, you can achieve advanced formatting.
Simple command chaining makes it possible to produce report-style output.

printf Debugging and Performance Optimization for Efficient Large-Scale Log Analysis

Create File

cat << 'EOF' > input.txt 2026-04-26 10:00:01 INFO user_id=101 action=login time=120 2026-04-26 10:00:02 INFO user_id=102 action=logout time=30 2026-04-26 10:00:03 INFO user_id=103 action=login time=200 2026-04-26 10:00:04 INFO user_id=101 action=login time=150 EOF

Command

awk '{ printf "USER:%s ACTION:%s TIME:%sms\n", $4, $5, $6 }' input.txt

Output

USER:user_id=101 ACTION:action=login TIME:time=120ms
USER:user_id=102 ACTION:action=logout TIME:time=30ms
USER:user_id=103 ACTION:action=login TIME:time=200ms
USER:user_id=101 ACTION:action=login TIME:time=150ms

Command

awk '{ split($6,a,"="); total+=a[2] } END { printf "TOTAL TIME: %dms\n", total }' input.txt

Output

TOTAL TIME: 500ms

Command

awk '$5=="action=login" { printf "%s %s\n", $4, $6 }' input.txt

Output

user_id=101 time=120
user_id=103 time=200
user_id=101 time=150

How It Works

ElementDescription
awkStream processing tool that handles text one line at a time
printfFast, formatted output with format specifiers
splitSplits a string to extract only the value part
ConditionQuickly filters only the needed log entries
Accumulator variableUses total+= to aggregate performance data

Explanation

Using printf in awk allows log formatting and debugging to be executed together at high speed.
By filtering out unnecessary log entries while aggregating data, the efficiency of large-scale log analysis improves dramatically.

Summary: Practical Data Formatting with awk and printf

Combining awk and printf enables advanced data formatting that goes beyond simple text processing.
Understanding the differences from print and mastering format specifiers, column alignment, and decimal control will significantly improve the quality of your output. Incorporating escape sequences, variables, and integration with external commands further extends its capabilities to handle real-world production-level tasks.
For beginners, the surest path to mastering these skills is to try each feature one at a time and build understanding gradually.

Leave a Reply

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

©︎ 2025-2026 running terminal commands