/[gentoo]/xml/htdocs/doc/en/articles/l-sed2.xml
Gentoo

Diff of /xml/htdocs/doc/en/articles/l-sed2.xml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.7 Revision 1.8
1<?xml version='1.0' encoding="UTF-8"?> 1<?xml version='1.0' encoding="UTF-8"?>
2<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/articles/l-sed2.xml,v 1.7 2011/09/04 17:53:41 swift Exp $ --> 2<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/articles/l-sed2.xml,v 1.8 2012/06/29 16:03:34 swift Exp $ -->
3<!DOCTYPE guide SYSTEM "/dtd/guide.dtd"> 3<!DOCTYPE guide SYSTEM "/dtd/guide.dtd">
4 4
5<guide disclaimer="articles"> 5<guide disclaimer="articles">
6<title>Sed by example, Part 2</title> 6<title>Sed by example, Part 2</title>
7 7
19<!-- The original version of this article was published on IBM developerWorks, 19<!-- The original version of this article was published on IBM developerWorks,
20and is property of Westtech Information Services. This document is an updated 20and is property of Westtech Information Services. This document is an updated
21version of the original article, and contains various improvements made by the 21version of the original article, and contains various improvements made by the
22Gentoo Linux Documentation team --> 22Gentoo Linux Documentation team -->
23 23
24<version>1.2</version> 24<version>2</version>
25<date>2005-10-09</date> 25<date>2005-10-09</date>
26 26
27<chapter> 27<chapter>
28<title>How to further take advantage of the UNIX text editor</title> 28<title>How to further take advantage of the UNIX text editor</title>
29<section> 29<section>
47not what you want. Normally, when I do a string replacement, I want to perform 47not what you want. Normally, when I do a string replacement, I want to perform
48it globally. That is, I want to replace all occurrences on every line, as 48it globally. That is, I want to replace all occurrences on every line, as
49follows: 49follows:
50</p> 50</p>
51 51
52<pre caption="Replacing all the occurences on every line"> 52<pre caption="Replacing all the occurrences on every line">
53$ <i>sed -e 's/foo/bar/g' myfile.txt</i> 53$ <i>sed -e 's/foo/bar/g' myfile.txt</i>
54</pre> 54</pre>
55 55
56<p> 56<p>
57The additional 'g' option after the last slash tells sed to perform a global 57The additional 'g' option after the last slash tells sed to perform a global
93slashes in it, we can change the separator by specifying a different character 93slashes in it, we can change the separator by specifying a different character
94after the 's'. For example, this will replace all occurrences of 94after the 's'. For example, this will replace all occurrences of
95<path>/usr/local</path> with <path>/usr</path>: 95<path>/usr/local</path> with <path>/usr</path>:
96</p> 96</p>
97 97
98<pre caption="Replacing all the occurences of one string with another one"> 98<pre caption="Replacing all the occurrences of one string with another one">
99$ <i>sed -e 's:/usr/local:/usr:g' mylist.txt</i> 99$ <i>sed -e 's:/usr/local:/usr:g' mylist.txt</i>
100</pre> 100</pre>
101 101
102<note> 102<note>
103In this example, we're using the colon as a separator. If you ever need to 103In this example, we're using the colon as a separator. If you ever need to
113 113
114<p> 114<p>
115Up until now, we've only performed simple string substitution. While this is 115Up until now, we've only performed simple string substitution. While this is
116handy, we can also match a regular expression. For example, the following sed 116handy, we can also match a regular expression. For example, the following sed
117command will match a phrase beginning with '&lt;' and ending with '&gt;', and 117command will match a phrase beginning with '&lt;' and ending with '&gt;', and
118containing any number of characters inbetween. This phrase will be deleted 118containing any number of characters in-between. This phrase will be deleted
119(replaced with an empty string): 119(replaced with an empty string):
120</p> 120</p>
121 121
122<pre caption="Deleting specified phrase"> 122<pre caption="Deleting specified phrase">
123$ <i>sed -e 's/&lt;.*&gt;//g' myfile.html</i> 123$ <i>sed -e 's/&lt;.*&gt;//g' myfile.html</i>
186The '[ ]' regular expression syntax has some more additional options. To specify 186The '[ ]' regular expression syntax has some more additional options. To specify
187a range of characters, you can use a '-' as long as it isn't in the first or 187a range of characters, you can use a '-' as long as it isn't in the first or
188last position, as follows: 188last position, as follows:
189</p> 189</p>
190 190
191<pre caption="Specifying a rangle of characters"> 191<pre caption="Specifying a range of characters">
192'[a-x]*' 192'[a-x]*'
193</pre> 193</pre>
194 194
195<p> 195<p>
196This will match zero or more characters, as long as all of them are 196This will match zero or more characters, as long as all of them are
255 </tr> 255 </tr>
256</table> 256</table>
257 257
258<p> 258<p>
259It's advantageous to use character classes whenever possible, because they adapt 259It's advantageous to use character classes whenever possible, because they adapt
260better to nonEnglish speaking locales (including accented characters when 260better to non-English speaking locales (including accented characters when
261necessary, etc.). 261necessary, etc.).
262</p> 262</p>
263 263
264</body> 264</body>
265</section> 265</section>

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.13