Home/Engineer/Linux / Shell/awk · sed in Practice — Log …
Linux / ShellIntermediateUbuntuDebianCentOSmacOSawksedlinux

awk · sed in Practice — Log Parsing and Text Transformation

A practical roundup of awk and sed patterns used every day on the job—log analysis, CSV conversion, config-file patching, and more.

Contents
  1. awk Basics
  2. Practical awk Log Analysis
  3. sed Basics
  4. Practical sed Config File Patching
  5. Combined Patterns

awk Basics

Bash
# Print fields by delimiter ($1 = first field)
awk '{print $1, $3}' access.log

# Specify delimiter (colon)
awk -F':' '{print $1}' /etc/passwd

# Conditional filter (rows where the 5th field is 200)
awk '$5 == 200 {print $0}' access.log

# Calculate a sum
awk '{sum += $NF} END {print "Total:", sum}' numbers.txt

Practical awk Log Analysis

Bash
# Aggregate by status code from Nginx access.log
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn

# Top 10 IPs by request count
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10

# Sum of response sizes (bytes)
awk '{sum += $10} END {printf "Total: %.2f MB\n", sum/1024/1024}' access.log

# Print 4xx and 5xx errors only
awk '$9 ~ /^[45]/' /var/log/nginx/access.log

sed Basics

Bash
# String substitution (first match)
sed 's/old/new/' file.txt

# Global substitution (g flag)
sed 's/old/new/g' file.txt

# Case-insensitive substitution
sed 's/old/new/gi' file.txt

# In-place file edit (-i)
sed -i 's/localhost/127.0.0.1/g' config.conf

# Delete a specific line (3rd line)
sed '3d' file.txt

# Remove empty lines
sed '/^$/d' file.txt

# Remove comment lines
sed '/^#/d' config.conf

Practical sed Config File Patching

Bash
# Change the port number
sed -i 's/^Port 22/Port 2222/' /etc/ssh/sshd_config

# Insert a new line after a matching line
sed -i '/^SELINUX=enforcing/a SELINUXTYPE=targeted' /etc/selinux/config

# Batch replace across multiple files
find . -name "*.conf" -exec sed -i 's/old_domain/new_domain/g' {} \;

# Use | instead of / as the delimiter (convenient when paths are involved)
sed 's|/old/path|/new/path|g' deploy.sh

Combined Patterns

Bash
# Process only a range of lines (lines 10–20)
sed -n '10,20p' large_file.log

# Print lines between two patterns
sed -n '/START/,/END/p' file.txt

# Parse with awk, then tidy the format with sed
awk -F',' '{print $1,$3}' data.csv | sed 's/ /\t/g'
#awk#sed#linux
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Linux man pages ↗

Questions & answers

Ask a question about this guide. We'll review and reply.