Sed 备忘单

sed unix 命令速查表

🧾 sed Command Cheatsheet (Stream Editor)

sed is a powerful Unix utility for parsing and transforming text using stream editing.

📌 Basic Syntax

sed [OPTIONS] 'script' [file...] 
  • 'script': One or more editing commands.
  • [file]: Input file(s). If omitted, sed reads from standard input.

⚙️ Common Options

OptionDescription
-nSuppress automatic printing of pattern space. Use p to print explicitly.
-eAdd multiple editing commands.
-iEdit files in-place (with optional backup: -i.bak).
-fRead commands from a file.

✂️ Basic Commands

CommandDescription
pPrint the current pattern space.
dDelete the current pattern space.
sSubstitute text using regex.
qQuit after processing first match.
aAppend text after current line.
iInsert text before current line.
cReplace line with new text.
yTranslate characters (like tr).

🔍 Substitution Syntax

sed 's/pattern/replacement/flags' file 

Flags

FlagDescription
gGlobal replacement (all matches in line).
iCase-insensitive matching.
pPrint the line if substitution occurred.
nReplace nth occurrence only.

Examples

sed 's/foo/bar/' file # Replace first 'foo' with 'bar' sed 's/foo/bar/g' file # Replace all 'foo' with 'bar' sed 's/foo/bar/2' file # Replace second 'foo' only sed 's/foo/bar/ip' file # Case-insensitive + print 

🗑️ Deletion with sed

sed can delete lines based on line numbers, patterns, or ranges.

🔢 Delete by Line Number

sed '2d' file # Delete line 2 sed '5,10d' file # Delete lines 5 through 10 

🔍 Delete by Pattern

sed '/error/d' file # Delete lines containing 'error' sed '/^$/d' file # Delete blank lines sed '/^#/d' file # Delete comment lines (starting with #) 

🧮 Delete by Range and Pattern

sed '1,/pattern/d' file # Delete from line 1 to first match of 'pattern' sed '/start/,/end/d' file # Delete lines between 'start' and 'end' (inclusive) 

🧠 Conditional Deletion

sed -n '/pattern/!p' file # Print only lines NOT matching 'pattern' 

🧹 Delete Last Line

sed '$d' file # Delete the last line 

📍 Addressing Lines

Line Numbers

sed '2d' file # Delete line 2 sed '3,5p' file # Print lines 3 to 5 

Patterns

sed '/error/d' file # Delete lines containing 'error' sed '/^#/d' file # Delete comment lines 

Combined

sed '1,/pattern/d' file # Delete from line 1 to first match of 'pattern' 

🧪 Advanced Substitution

Using Capture Groups

sed 's/\(foo\)bar/\1baz/' file 
  • \(...\) captures a group.
  • \1, \2, etc., refer to captured groups.

Escaping Special Characters

sed 's/\/usr\/bin/\/usr\/local\/bin/' file 

Or use alternate delimiter:

sed 's|/usr/bin|/usr/local/bin|' file 

🧨 In-Place Editing

sed -i 's/foo/bar/g' file # Edit file directly sed -i.bak 's/foo/bar/g' file # Backup original as file.bak 

📂 Multiple Commands

Inline

sed -e 's/foo/bar/' -e '/baz/d' file 

Block Syntax

sed ' s/foo/bar/ s/baz/qux/ ' file 

🧵 Append, Insert, Change

sed '/pattern/a\Text to append' file sed '/pattern/i\Text to insert' file sed '/pattern/c\New line content' file 

🔄 Translate Characters

sed 'y/abc/ABC/' file # a→A, b→B, c→C 

🧠 Useful Recipes

Remove Blank Lines

sed '/^$/d' file 

Remove Leading/Trailing Whitespace

sed 's/^[ \t]*//' file # Remove leading sed 's/[ \t]*$//' file # Remove trailing 

Replace Tabs with Spaces

sed 's/\t/ /g' file 

Number Lines

sed = file | sed 'N;s/\n/\t/' 

📚 Resources

相关工具 (17)

留言区
昵称
邮箱
网址
0/500
0 条评论
没有评论
查看更多
Powered by Twikoo v1.6.44
Twikoo 评论管理
密码