Which language do you prefer? English 简体中文 繁体中文
...

Part of Linux Commands Cheat Sheet

Search And Replace


echo 'Hello World' | sed 's/World/world/'

Output:


Hello world

Operating file:


sed 's/World/world/' filename

Merge Multiple Commands


echo 'Hello World' | sed 's/World/world/; s/Hello/hello/'

Output:


hello world

Flags


echo 'Hello World' | sed 's/o/O/'

Output:


HellO World

echo 'Hello World' | sed 's/o/O/g'

Output:


HellO WOrld

Limiting sed

Only second line:


sed '2s/search/replace/' filename

From second line to forth line:


sed '2,4s/search/replace/' filename

From second line to the end of the file:


sed '2,$s/search/replace/' filename

Lines that satisfied specific pattern:


sed '/pattern/s/search/replace/' filename

From line that satisfied a pattern to line that satisfied another pattern:


sed '/pattern1/,/pattern2/s/search/replace/' filename

Delete Lines


sed '2d' filename
sed '2,4d' filename
sed '2,$d' filename
sed '/pattern/d' filename
sed '/pattern1/,/pattern2/d' filename

Insert And Append Lines


sed 'i\new line' filename
sed 'a\new line' filename
sed '2i\new line' filename
sed '2a\new line' filename
sed '/pattern/i\new line' filename
sed '/pattern/a\new line' filename

Change Lines


sed '1c\new line' filename
sed '/pattern/c\new line' filename

Modify Characters


sed 'y/abc/xyz/' filename

Print Line Numbers


sed '=' filename
sed '/pattern/=' filename

31+ Examples For Sed Linux Command In Text Manipulation - Like Geeks

wbswjc-blog