| 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, |
| 20 | and is property of Westtech Information Services. This document is an updated |
20 | and is property of Westtech Information Services. This document is an updated |
| 21 | version of the original article, and contains various improvements made by the |
21 | version of the original article, and contains various improvements made by the |
| 22 | Gentoo Linux Documentation team --> |
22 | Gentoo 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> |
| … | |
… | |
| 47 | not what you want. Normally, when I do a string replacement, I want to perform |
47 | not what you want. Normally, when I do a string replacement, I want to perform |
| 48 | it globally. That is, I want to replace all occurrences on every line, as |
48 | it globally. That is, I want to replace all occurrences on every line, as |
| 49 | follows: |
49 | follows: |
| 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> |
| 57 | The additional 'g' option after the last slash tells sed to perform a global |
57 | The additional 'g' option after the last slash tells sed to perform a global |
| … | |
… | |
| 93 | slashes in it, we can change the separator by specifying a different character |
93 | slashes in it, we can change the separator by specifying a different character |
| 94 | after the 's'. For example, this will replace all occurrences of |
94 | after 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> |
| 103 | In this example, we're using the colon as a separator. If you ever need to |
103 | In this example, we're using the colon as a separator. If you ever need to |
| … | |
… | |
| 113 | |
113 | |
| 114 | <p> |
114 | <p> |
| 115 | Up until now, we've only performed simple string substitution. While this is |
115 | Up until now, we've only performed simple string substitution. While this is |
| 116 | handy, we can also match a regular expression. For example, the following sed |
116 | handy, we can also match a regular expression. For example, the following sed |
| 117 | command will match a phrase beginning with '<' and ending with '>', and |
117 | command will match a phrase beginning with '<' and ending with '>', and |
| 118 | containing any number of characters inbetween. This phrase will be deleted |
118 | containing 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/<.*>//g' myfile.html</i> |
123 | $ <i>sed -e 's/<.*>//g' myfile.html</i> |
| … | |
… | |
| 186 | The '[ ]' regular expression syntax has some more additional options. To specify |
186 | The '[ ]' regular expression syntax has some more additional options. To specify |
| 187 | a range of characters, you can use a '-' as long as it isn't in the first or |
187 | a range of characters, you can use a '-' as long as it isn't in the first or |
| 188 | last position, as follows: |
188 | last 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> |
| 196 | This will match zero or more characters, as long as all of them are |
196 | This 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> |
| 259 | It's advantageous to use character classes whenever possible, because they adapt |
259 | It's advantageous to use character classes whenever possible, because they adapt |
| 260 | better to nonEnglish speaking locales (including accented characters when |
260 | better to non-English speaking locales (including accented characters when |
| 261 | necessary, etc.). |
261 | necessary, etc.). |
| 262 | </p> |
262 | </p> |
| 263 | |
263 | |
| 264 | </body> |
264 | </body> |
| 265 | </section> |
265 | </section> |