Command-line Knowledge Base

sed

Use a sed script/file
sed -i -f commands.sed outputfile.txt
Ranges by patterns

You can specify two regular expressions as the range. Assuming a "#" starts a comment, you can search for a keyword, remove all comments until you see the second keyword. In this case the two keywords are "start" and "stop:"

sed '/start/,/stop/ s/#.*//'
sed '/<\/head>/,/.*<\/h1>/ d'
To add text to the end of a file [1][2]
sed -i -e "\$a some text" $file
Using variables in sed commands [3]

Use double quotes to get the shell to expand variables.

sed -i "s/$var1/ZZ/g" "$file"
Replace one or more digits [1]
sed 's/slide[0-9]*/slide/' links.html
Replace one or more digits [2] [5]
sed 's/ id="ingredient_[0-9]*"//' test.html
Replace newlines with a space [4]
sed ':a;N;$!ba;s/\n/ /g' file
Delete HTML Tags [6]
sed 's/<[^>]*>//g'
Simple Multiline Tag Matching

To replace:

<p>Some important paragraph text</p>
<ul>
<ul>
<ul>
<li>Some important list item</li>

With:

<p>Some important paragraph text</p>

<ul>
<li>Some important list item</li>

Use:

sed 'N;N;s/<\/p>\n<ul>\n<ul>/<\/p>\n/'
Delete two blank lines in a row [7]*

* This article does a good job of explaining how sed works. Also gives an example for deleting three blank lines in a row.

sed 'N;/^\n$/d;P:D' $file
Remove the first word of a line with sed (1/2) [8] [9]
root@linux:~# echo "first second third fourth fifth" | sed "s/^[^ ]* //"
second third fourth fifth
sed 's/\(^[^ ]\)* /<p>\1/' twelve_steps__multi_purpose.html
Replace an entire line by line number
sed '5s/.*/boo/' 00__template__recipe.php
Remove a Range of Lines 10
sed '4,7d' file
NULL