Tutorial Lengkap Sed untuk Text Processing di Linux
Tutorial Lengkap Sed untuk Text Processing di Linux
Sed (Stream Editor) adalah text processing utility yang powerful untuk melakukan basic transformations pada input stream. Meski tidak se-powerful AWK untuk complex processing, Sed unggul dalam text substitution dan manipulation.
1. Sintaks Dasar Sed
Struktur Command
sed [options] 'command' file
sed [options] -e 'command1' -e 'command2' file
Command Fundamental
# Print semua baris (default behavior)
sed '' file.txt
sed -n 'p' file.txt
# Print baris tertentu
sed -n '5p' file.txt # Line 5
sed -n '5,10p' file.txt # Lines 5-10
sed -n '$p' file.txt # Last line
sed -n '1~2p' file.txt # Every 2nd line starting from 1
# Print multiple ranges
sed -n '1p;5p;10p' file.txt
2. Substitution dengan Sed
Basic Substitution
# Replace text (hanya first occurrence per line)
sed 's/old/new/' file.txt
# Replace all occurrences dalam line (global)
sed 's/old/new/g' file.txt
# Replace specific occurrence
sed 's/old/new/2' file.txt # Replace 2nd occurrence
sed 's/old/new/2g' file.txt # Replace 2nd dan seterusnya
# Use different delimiters (useful untuk paths)
sed 's|/usr/local|/opt|g' file.txt
sed 's#/home/user#/data#g' file.txt
sed 's_old_new_g' file.txt
Advanced Substitution
# Case insensitive
sed 's/old/new/gi' file.txt
# Only print modified lines
sed -n 's/old/new/gp' file.txt
# Capture groups dengan backreferences
sed 's/\(.*\):\(.*\)/\2: \1/' file.txt
# Replace dengan line number
sed = file.txt | sed 'N; s/\n/ /'
# Replace dengan environment variable
sed "s/USER/$USER/g" file.txt
# Replace only specific lines
sed '5s/old/new/' file.txt # Line 5 only
sed '/pattern/s/old/new/' file.txt # Lines matching pattern
sed '1,10s/old/new/' file.txt # Lines 1-10
3. Deletion dan Insertion
Deleting Lines
# Delete baris tertentu
sed '5d' file.txt # Delete line 5
sed '5,10d' file.txt # Delete lines 5-10
sed '$d' file.txt # Delete last line
sed '1d' file.txt # Delete first line
# Delete based on pattern
sed '/pattern/d' file.txt # Delete lines with pattern
sed '/pattern/!d' file.txt # Delete lines NOT matching pattern
# Delete blank lines
sed '/^$/d' file.txt
sed '/^[[:space:]]*$/d' file.txt
# Delete lines dengan whitespace only
sed '/^[[:space:]]*$/d' file.txt
Inserting dan Appending
# Insert before line
sed '5i\This is new line' file.txt
# Append after line
sed '5a\This is appended' file.txt
# Insert multiple lines
sed '5i\
Line 1\
Line 2\
Line 3' file.txt
# Append after last line
sed '$a\This is last' file.txt
# Insert at beginning of file
sed '1i\# Header' file.txt
4. Addressing dan Ranges
Line Addressing
# Single line
sed '5s/old/new/' file.txt
# Line range
sed '5,10s/old/new/' file.txt
# From line to end
sed '5,$s/old/new/' file.txt
# Pattern matching
sed '/start/,/end/s/old/new/' file.txt
# Regex pattern
sed '/^#/s/old/new/' file.txt
# Multiple addresses
sed -e '1s/^/START/' -e '$s/$/END/' file.txt
Range Operations
# Process range
sed -n '/START/,/END/p' file.txt
# Delete range
sed '/START/,/END/d' file.txt
# Substitute dalam range
sed '/START/,/END/s/foo/bar/' file.txt
5. Advanced Sed Techniques
Multiple Commands
# Multiple sed commands
sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt
# Command file
sed -f commands.sed file.txt
# Commands dalam file commands.sed:
# s/foo/bar/
# s/baz/qux/
# 5d
In-place Editing
# Edit file in-place (backup original)
sed -i.bak 's/foo/bar/g' file.txt
# Edit file in-place tanpa backup
sed -i 's/foo/bar/g' file.txt
# Edit multiple files
sed -i 's/foo/bar/g' *.txt
Working dengan Multiple Lines
# Print next line juga
sed -n '/pattern/{N;p}' file.txt
# Join lines
sed ':a;N;$!ba;s/\n/ /g' file.txt
# Delete newlines
sed ':a;N;$!ba;s/\n//g' file.txt
# Merge every 2 lines
sed 'N;s/\n/ /' file.txt
# Swap line order
sed 'n;G;h;P;d' file.txt
Case Conversion
# Convert ke uppercase
sed 's/.*/\U&/' file.txt
# Convert ke lowercase
sed 's/.*/\L&/' file.txt
# Capitalize first letter
sed 's/.*/\u&/' file.txt
# Title case (setiap word)
sed 's/\b\([a-z]\)/\u\1/g' file.txt
6. Praktis One-Liners
File Manipulation
# Remove comments dan blank lines
sed '/^[[:space:]]*#/d; /^[[:space:]]*$/d' config.conf
# Add line numbers
sed = file.txt | sed 'N; s/\n/ /'
# Double space file
sed G file.txt
# Reverse lines of file
sed '1!G;h;$!d' file.txt
# Remove trailing whitespace
sed 's/[[:space:]]*$//' file.txt
# Remove leading whitespace
sed 's/^[[:space:]]*//' file.txt
# Remove both leading dan trailing
sed 's/^[[:space:]]*//;s/[[:space:]]*$//' file.txt
# Indent each line dengan 4 spaces
sed 's/^/ /' file.txt
Data Processing
# Convert spaces ke tabs
sed 's/ */ /g' file.txt
# Convert tabs ke spaces
sed 's/ / /g' file.txt
# Remove ANSI color codes
sed 's/\x1B\[[0-9;]*[JKmsu]//g' file.txt
# URL encode
sed 's/ /%20/g' file.txt
# Wrap long lines at 80 characters
sed -n -e ':a' -e 's/^\(.\{80\}\)/\1\\/;ta' -e 'p' file.txt
Configuration File Editing
# Uncomment line
sed -i 's/^#Port 22/Port 22/' /etc/ssh/sshd_config
# Change value
sed -i 's/^Port 22/Port 2222/' /etc/ssh/sshd_config
# Add setting if not exists
sed -i '/^#*Port 22/!a\Port 2222' /etc/ssh/sshd_config
# Delete setting
sed -i '/^PermitRootLogin yes/d' /etc/ssh/sshd_config
# Replace dalam tag HTML
sed 's/<[^>]*>//g' file.html
Kesimpulan
Sed adalah tools yang sangat powerful untuk text transformation. Meski learning curve sedikit curam, menguasai Sed akan menghemat banyak waktu dalam text processing tasks.
Kapan Menggunakan Sed vs AWK:
- Sed: Simple text substitution, deletion, insertion
- AWK: Field-based processing, calculations, complex logic
- Both: Often used together dalam pipelines
Tips:
- Selalu test dengan
-ndanpsebelum in-place editing - Gunakan backup (
-i.bak) untuk file penting - Kombinasikan dengan pipes untuk workflow complex
- Gunakan command files untuk scripts panjang
Alternatives:
tr: Simple character translationcut: Field extraction sederhanaperl -pe: Perl one-liners (more powerful)awk: Field-based processing
Artikel Terkait
Link Postingan: https://www.tirinfo.com/tutorial-lengkap-sed-text-processing-linux/