Difference between revisions of "sed sed"

From Noah.org
Jump to navigationJump to search
Line 7: Line 7:
  
 
This finds line that begins with exit(ignore leading spaces), then insert 'authdarmond start' before it.
 
This finds line that begins with exit(ignore leading spaces), then insert 'authdarmond start' before it.
 +
 
   sed -i -e "/^\\s*exit/i/authdaemond start" /etc/rc.local;
 
   sed -i -e "/^\\s*exit/i/authdaemond start" /etc/rc.local;
  
Line 12: Line 13:
  
 
Note how replace pattern spans multiple lines
 
Note how replace pattern spans multiple lines
 +
 
<pre>sed -i -e '/#!\/bin\/sh/a\
 
<pre>sed -i -e '/#!\/bin\/sh/a\
 
QMAILQUEUE="/var/qmail/bin/qmail-scanner-queue.pl" ; export QMAILQUEUE' /var/qmail/supervise/qmail-smtpd/run
 
QMAILQUEUE="/var/qmail/bin/qmail-scanner-queue.pl" ; export QMAILQUEUE' /var/qmail/supervise/qmail-smtpd/run
 +
</pre>
 +
 +
== search and replace in multiple files ==
 +
 +
This will perform a search and replace on all files in a directory tree:
 +
 +
<pre>
 +
find ./ -type f -exec sed -i -e "s/find_this_text/replace_with_this_text/" {} \;
 
</pre>
 
</pre>
  

Revision as of 14:43, 15 January 2008

find part of a line, substitute, keep rest of time intact

 sed -i -e "s/^#DatabaseDirectory \(.*\)/DatabaseDirectory \\1/" myfile.txt

find pattern in a line, insert new line before it

This finds line that begins with exit(ignore leading spaces), then insert 'authdarmond start' before it.

 sed -i -e "/^\\s*exit/i/authdaemond start" /etc/rc.local;

find pattern and append line

Note how replace pattern spans multiple lines

sed -i -e '/#!\/bin\/sh/a\
QMAILQUEUE="/var/qmail/bin/qmail-scanner-queue.pl" ; export QMAILQUEUE' /var/qmail/supervise/qmail-smtpd/run

search and replace in multiple files

This will perform a search and replace on all files in a directory tree:

find ./ -type f -exec sed -i -e "s/find_this_text/replace_with_this_text/" {} \;

sed documentation

This is one of the best sed documents I found: Grymoire Sed