<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://www.freebsdwiki.net/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://www.freebsdwiki.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Judy123</id>
		<title>FreeBSDwiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://www.freebsdwiki.net/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Judy123"/>
		<link rel="alternate" type="text/html" href="http://www.freebsdwiki.net/index.php/Special:Contributions/Judy123"/>
		<updated>2026-04-05T23:55:32Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.18.0</generator>

	<entry>
		<id>http://www.freebsdwiki.net/index.php/Sed</id>
		<title>Sed</title>
		<link rel="alternate" type="text/html" href="http://www.freebsdwiki.net/index.php/Sed"/>
				<updated>2010-06-28T08:19:54Z</updated>
		
		<summary type="html">&lt;p&gt;Judy123: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Short for &amp;quot;Stream Editor&amp;quot;, '''sed''' allows you to run a stream (for example, the contents of a file, or the output of a program) through it and either match or change data. This can be particularly handy if you need to make a lot of similar changes to a file -- e.g., you find out you consistently put in a wrong hostname in a configuration file, and you need to change your conf file to point to the right server....except the same change needs to be made 100 times. [[sed]] to the rescue.&lt;br /&gt;
&lt;br /&gt;
Note that the change will only happen on the first instance of the phrase in each line.&lt;br /&gt;
&lt;br /&gt;
The most common usage is to change all instances of a phrase to another; to wit:&lt;br /&gt;
&lt;br /&gt;
 dave@samizdata:~% '''cat sed_testfile'''&lt;br /&gt;
  dave&lt;br /&gt;
  dave&lt;br /&gt;
  davedave&lt;br /&gt;
  jimbo&lt;br /&gt;
  dave&lt;br /&gt;
  freebsd&lt;br /&gt;
 dave@samizdata:~% '''sed s/dave/david/ sed_testfile'''&lt;br /&gt;
  david&lt;br /&gt;
  david&lt;br /&gt;
  daviddave&lt;br /&gt;
  jimbo&lt;br /&gt;
  david&lt;br /&gt;
  freebsd&lt;br /&gt;
 dave@samizdata:~% '''more sed_testfile'''&lt;br /&gt;
  dave&lt;br /&gt;
  dave&lt;br /&gt;
  davedave&lt;br /&gt;
  jimbo&lt;br /&gt;
  dave&lt;br /&gt;
  freebsd&lt;br /&gt;
 dave@samizdata:~%&lt;br /&gt;
&lt;br /&gt;
As you can see, no changes were made to the file itself - '''sed''' merely wrote the edited stream to standard output.  You can easily put the changed data in a new file with the help of [[redirection]]:&lt;br /&gt;
&lt;br /&gt;
[http://www.superiorpapers.com custom term papers]&lt;br /&gt;
 dave@samizdata:~% '''sed s/dave/david/ sed_testfile &amp;gt; sed_testfile_new'''&lt;br /&gt;
 dave@samizdata:~% '''more sed_testfile_new'''&lt;br /&gt;
  david&lt;br /&gt;
  david&lt;br /&gt;
  daviddave&lt;br /&gt;
  jimbo&lt;br /&gt;
  david&lt;br /&gt;
  freebsd&lt;br /&gt;
 dave@samizdata:~%&lt;br /&gt;
&lt;br /&gt;
You'll also note that it only changed the first instance of the word per line; to change all instances on each line, you'll want to use the &amp;quot;g&amp;quot; modifier (to make '''global''' switches) like this:&lt;br /&gt;
 dave@samizdata:~% sed s/dave/david/g sed_testfile &amp;gt; sed_testfile_new&lt;br /&gt;
See the man page for more details.&lt;br /&gt;
&lt;br /&gt;
To give you another example of how useful [[sed]] can be, let's say that you have a DHCP server and want to change a whole segment's range:&lt;br /&gt;
&lt;br /&gt;
 samizdata# '''sed s/10.1.0./10.2.0./ dhcpd.conf &amp;gt; new_dhcpd.conf'''&lt;br /&gt;
&lt;br /&gt;
Will give you a new_dhcpd.conf file with the changes made. You'll want to check the file by hand to make sure that the changes are correct, but you just saved yourself a lot of typing :)&lt;br /&gt;
&lt;br /&gt;
'''sed''' can also be used to make changes to a file or files directly, while backing up the original version of the file(s).  This is done using the -i argument, along with the desired suffix for backed up files.  For example:&lt;br /&gt;
&lt;br /&gt;
 samizdata# '''sed -i .bak s/10.1.0./10.2.0./ dhcpd.conf'''&lt;br /&gt;
 samizdata# '''ls | grep dhcpd'''&lt;br /&gt;
 dhcpd.conf&lt;br /&gt;
 dhcpd.conf.bak&lt;br /&gt;
&lt;br /&gt;
Note that the standard delimiter is &amp;quot;/&amp;quot; but it can be just about anything; %, _, or even commas. So changing a file with lots of forward-slashes (/) can be as ugly as&lt;br /&gt;
 &amp;gt; sed s/\/dev\/am0/\/dev\/gm0/g example.fstab &amp;gt; example.fstab.new&lt;br /&gt;
or as simple as&lt;br /&gt;
 &amp;gt; sed s%/dev/am0%/dev/gm0%g example.fstab &amp;gt; example.fstab.new&lt;br /&gt;
&lt;br /&gt;
If you want to use [[regular expression]]s with '''sed''', you should install the [[gsed]] [[port]] - FreeBSD's native '''sed''' does not handle anything more complicated than a wildcard the way you'd expect it to.  For example, say you want to use the '''\b''' (word boundary) regex code to change instances of &amp;quot;david&amp;quot; to &amp;quot;dave&amp;quot;, ''without'' breaking any instances of &amp;quot;davidson&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 samizdata# '''echo david davidson | sed s/david/dave/g'''&lt;br /&gt;
  dave daveson&lt;br /&gt;
&lt;br /&gt;
Not what we want!  So we'll use the standard '''\b''' (word boundary) regex code to make sure we don't change words that begin with &amp;quot;david&amp;quot;:&lt;br /&gt;
&lt;br /&gt;
 samizdata# '''echo david davidson | sed &amp;quot;s/david\b/dave/g&amp;quot;'''&lt;br /&gt;
  david davidson&lt;br /&gt;
&lt;br /&gt;
Not what we expected!  But if we install [[gsed]] from [[ports]] and use it instead:&lt;br /&gt;
&lt;br /&gt;
 samizdata# '''cd /usr/ports/textproc/gsed &amp;amp;&amp;amp; make install clean &amp;amp;&amp;amp; [[rehash]]'''&lt;br /&gt;
 samizdata# '''echo david davidson | gsed &amp;quot;s/david\b/dave/g&amp;quot;'''&lt;br /&gt;
  dave davidson&lt;br /&gt;
&lt;br /&gt;
Much better!  You may be tempted to simply overwrite the base sed with [[gsed]], but carefully consider it before you do so: if you have any utilities installed which depend on FreeBSD's native implementations differences in regex (or flag) handling, you could break them.  This generally should not be a problem, but BACK UP THE ORIGINAL VERSION of FreeBSD's '''sed''' before overwriting it with [[gsed]] if you decide to do so.&lt;br /&gt;
&lt;br /&gt;
For more info, check out these pages: &lt;br /&gt;
&lt;br /&gt;
http://www.student.northpark.edu/pemente/sed/&lt;br /&gt;
&lt;br /&gt;
http://www.faqs.org/faqs/editor-faq/sed/&lt;br /&gt;
&lt;br /&gt;
http://www.grymoire.com/Unix/Sed.html&lt;br /&gt;
&lt;br /&gt;
[[Category:System Commands]]&lt;/div&gt;</summary>
		<author><name>Judy123</name></author>	</entry>

	</feed>