================================================================================ SED COMMAND CHEAT SHEET - QUICK REFERENCE ================================================================================ SED (Stream EDitor) - powerful text processing utility for filtering and transforming text. Basic Syntax: sed [options] 'command' file.txt command | sed [options] 'command' COMMON OPTIONS -------------------------------------------------------------------------------- -i[suffix] Edit files in-place (with optional backup) -e Specify multiple commands -f script Take commands from a file -n Suppress automatic printing of pattern space -r, -E Use extended regular expressions ADDRESSING - SELECTING LINES -------------------------------------------------------------------------------- n Line number sed '2s/foo/bar/' file.txt $ Last line sed '$s/foo/bar/' file.txt n,m Line range sed '2,5s/foo/bar/' file.txt n,+m n and next m lines sed '2,+3s/foo/bar/' file.txt /pat/ Lines matching pattern sed '/error/s/foo/bar/' file.txt /pat/,/pat/ Range between patterns sed '/start/,/end/s/foo/bar/' file.txt addr! All except matching lines sed '/error/!s/foo/bar/' file.txt BASIC TEXT OPERATIONS -------------------------------------------------------------------------------- s/old/new/ Replace first occurrence sed 's/error/warning/' file.txt s/old/new/g Replace all occurrences sed 's/error/warning/g' file.txt s/old/new/gi Case-insensitive replace sed 's/Error/warning/gi' file.txt s/old/new/2 Replace 2nd occurrence sed 's/error/warning/2' file.txt s/old/new/2g Replace 2nd & subsequent sed 's/error/warning/2g' file.txt s|old|new|g Alternative delimiters sed 's|/var/log|/opt/log|g' file.txt LINE OPERATIONS -------------------------------------------------------------------------------- d Delete lines sed '/error/d' file.txt 5d Delete specific line sed '5d' file.txt 5,10d Delete line range sed '5,10d' file.txt /pat/d Delete matching lines sed '/DEBUG/d' file.txt p Print lines sed -n '/ERROR/p' file.txt a\text Append after line sed '/pattern/a\New line' file.txt i\text Insert before line sed '/pattern/i\New line' file.txt c\text Change/replace entire line sed '/pattern/c\Replacement' file.txt ADVANCED OPERATIONS -------------------------------------------------------------------------------- s/(.+)/\1/ Capture groups (basic regex) sed 's/\(user\) \(name\)/\2, \1/' file.txt s/(.+)/\1/ Capture groups (extended) sed -E 's/(user) (name)/\2, \1/' file.txt & Reuse matched pattern sed 's/user/[&]/' file.txt = Print line number sed = file.txt N Append next line to pattern sed 'N; s/\n/ /' file.txt P Print first part until \n sed 'N; P; D' file.txt D Delete first part until \n sed 'N; P; D' file.txt h Copy pattern to hold space sed 'h; s/a/A/g; H; g' file.txt H Append pattern to hold space sed 'h; s/a/A/g; H; g' file.txt g Get hold space, copy to pat. sed 'h; s/a/A/g; H; g' file.txt G Get hold space, append to pat. sed 'G' file.txt MULTIPLE OPERATIONS -------------------------------------------------------------------------------- cmd1; cmd2 Execute multiple commands sed 's/old/new/g; s/foo/bar/g' file.txt {cmds} Group commands sed '/match/{s/old/new/g; d}' file.txt COMMON PRACTICAL EXAMPLES -------------------------------------------------------------------------------- • Remove empty lines: sed '/^$/d' file.txt • Remove leading whitespace: sed 's/^[ \t]*//' file.txt • Remove trailing whitespace: sed 's/[ \t]*$//' file.txt • Convert DOS/Windows to Unix line endings: sed 's/\r$//' file.txt • Remove comments: sed 's/#.*$//' file.txt • Extract lines between two patterns (inclusive): sed -n '/START_PATTERN/,/END_PATTERN/p' file.txt • Number each line: sed = file.txt | sed 'N;s/\n/\t/' • Replace in multiple files: sed -i 's/old/new/g' *.txt • Add text at beginning of file: sed '1i\# This is a header' file.txt • Add text at end of file: sed '$a\# This is a footer' file.txt • Replace only on specific section: sed '/START/,/END/{s/foo/bar/g}' file.txt SPECIAL CHARACTERS (NEED ESCAPING) -------------------------------------------------------------------------------- \ / . * [ ] ^ $ & COMMON REGEX PATTERNS -------------------------------------------------------------------------------- . Any single character * Zero or more of previous character .* Zero or more of any character \+ One or more of previous character (extended regex) \? Zero or one of previous character (extended regex) ^ Start of line $ End of line [abc] Any character in the set [^abc] Any character not in the set [a-z] Any character in the range \1, \2 Back-references to capture groups TIPS AND BEST PRACTICES -------------------------------------------------------------------------------- • Test without -i first to preview changes • Always create backups with -i.bak for important files • Use different delimiters (|, :, #) when pattern contains / • For complex operations, consider writing a sed script file • Chain simple operations instead of writing complex ones • Use extended regex (-E) for cleaner syntax with capturing groups ================================================================================