-
-
Notifications
You must be signed in to change notification settings - Fork 2
Linux sed awk Guide
Complete beginner-friendly guide to sed and awk on Linux, covering Arch Linux, CachyOS, and other distributions including text processing, pattern matching, and data manipulation.
- Understanding sed and awk
- sed Basics
- sed Advanced
- awk Basics
- awk Advanced
- Practical Examples
- Troubleshooting
sed (stream editor) edits text streams.
Uses:
- Find and replace: Replace text in files
- Delete lines: Remove specific lines
- Insert text: Add text to files
- Batch editing: Edit multiple files
awk is a pattern scanning and processing language.
Uses:
- Data extraction: Extract fields from text
- Text processing: Process structured data
- Report generation: Generate formatted reports
- Data analysis: Analyze text data
sed is pre-installed:
# Check version
sed --version
# Usually included in base systemReplace text:
# Replace first occurrence
sed 's/old/new/' file.txt
# Replace all occurrences
sed 's/old/new/g' file.txt
# Case insensitive
sed 's/old/new/gi' file.txtEdit file directly:
# Edit file in place
sed -i 's/old/new/g' file.txt
# Backup original
sed -i.bak 's/old/new/g' file.txtRemove lines:
# Delete line 5
sed '5d' file.txt
# Delete lines 2-5
sed '2,5d' file.txt
# Delete lines matching pattern
sed '/pattern/d' file.txtAdd text:
# Insert before line 3
sed '3i\New line' file.txt
# Append after line 3
sed '3a\New line' file.txtawk is pre-installed:
# Check version
awk --version
# Usually included in base systemExtract columns:
# Print first field
awk '{print $1}' file.txt
# Print multiple fields
awk '{print $1, $3}' file.txt
# Print all fields
awk '{print $0}' file.txtCustom separator:
# Use comma as separator
awk -F',' '{print $1}' file.txt
# Use colon
awk -F':' '{print $1}' /etc/passwdMatch patterns:
# Print lines matching pattern
awk '/pattern/ {print}' file.txt
# Print lines where field matches
awk '$1 == "value" {print}' file.txtPerform calculations:
# Sum column
awk '{sum += $1} END {print sum}' file.txt
# Average
awk '{sum += $1; count++} END {print sum/count}' file.txtCommon tasks:
# Remove blank lines
sed '/^$/d' file.txt
# Remove leading spaces
sed 's/^[ \t]*//' file.txt
# Remove trailing spaces
sed 's/[ \t]*$//' file.txtCommon tasks:
# Print lines longer than 80 characters
awk 'length > 80' file.txt
# Count lines
awk 'END {print NR}' file.txt
# Print unique values in column
awk '{print $1}' file.txt | sort -uCheck syntax:
# Test without modifying
sed 's/old/new/g' file.txt
# Check for errors
sed -n 's/old/new/p' file.txtDebug awk:
# Check field numbers
awk '{print NF, $0}' file.txt
# Check field separator
awk -F',' '{print NF}' file.txtThis guide covered sed and awk installation, basic usage, and advanced features for Arch Linux, CachyOS, and other distributions.
- Grep Command Guide - Text searching
- Text Editors - Text editors
-
sed Documentation:
man sed -
awk Documentation:
man awk
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.