Home Linux Utils
use -i to edit the current file directly instead of going to stdout.

get line 28

sed '28q;d'
 

remove first 3 characters

sed 's/^...//'


remove last 3 characters

sed 's/...$//' 


remove trailing and leading spaces

sed -e 's/ *//' -e 's/ *$//'


All to lowercase

sed -e 's/\(.*\)/\L\1/'


with a variable

sed "s/changeMe/$i/" 


Change 3rd occurance of search

sed s/old/new/3 


replace special characters

echo "*test/test1/test2$" | sed 's#\([[]]\*\$\/&[[]]\)#\\&#g'

& refer to  that portion of the pattern which matched

Keep the first word of the line and delete the rest

sed 's/\(myFirstWord\).*/\1/'


Replace the whole line containing the word

sed "s/.*wordMatch.*/substitute with string/"


Add a line or multiple lines after my match

 sed -i '
  /<MYMATCHLINE>/ a\
  \    line 1\
  \ line 2
 ' /path/to/file



Add a line or multiple lines before my match

 sed -i '
  /<MYMATCHLINE>/ i\
  \    line 1\
  \ line 2
 ' /path/to/file



Add a line or multiple lines containing the value of a variable from the calling script.  
Before match example 

 sed -i "
 /<MYMATCHLINE>/ i\
 \    line 1 with var $1\\n\
 \ line 2
" /path/to/file

\\n is for adding a new line char. Only needed in "  not '

to test  c\  would change the line. ie change a whole line on one keyword