Difference between revisions of "Sed"

From the Linux and Unix Users Group at Virginia Teck Wiki
Jump to: navigation, search
imported>Pew
(Replaced content with "Category:Pending deletion")
(Undo revision 1837 by [[Special:Contributions/imported>Pew|imported>Pew]] ([[User talk:imported>Pew|talk]]))
(Tag: Undo)
 
Line 1: Line 1:
[[Category:Pending deletion]]
+
'''Sed''' is an extremely handy commandline *nix tool. Refer to its manual page for a more extensive overview of its capabilities.
 +
 
 +
=Uses=
 +
==Substitution==
 +
The most popular command of sed is the <code>s</code> command for substitution. To use the command, type an s, followed by a delimiting character, followed by a regular expression to look for, followed by the delimiting character, followed by an expression with which to replace what was found, followed by a final delimiting character. (<code>/</code> is the conventional delimiting character and potentially required by some versions of sed. GNU sed can take any character however, and when URLs or paths are involved, <code>%</code> is often a handy choice to lessen the amount of escaping required.)
 +
<pre>
 +
$ echo "Never let schooling interfere with your education." | sed 's/schooling/video games/'
 +
Never let video games interfere with your education.
 +
</pre>
 +
 
 +
Groups can be useful in the replacement portion. Use the <code>-r</code> flag if you don't want to escape the initial grouping parenthesis. The <code>-e</code> flag allows for multiple expressions to be evaluated.
 +
<pre>
 +
$ echo "Never let schooling interfere with your education." | sed -re 's/(schooling)/costly \1/' -e 's/(education)/invaluable \1/'
 +
Never let costly schooling interfere with your invaluable education.
 +
</pre>
 +
 
 +
=Examples=
 +
* [[Reencoding MediaWiki pages#Script]]
 +
* [[Central Authentication System#Scripted Login]]
 +
 
 +
[[Category:Software]]
 +
[[Category:Howtos]]

Latest revision as of 02:37, 4 January 2019

Sed is an extremely handy commandline *nix tool. Refer to its manual page for a more extensive overview of its capabilities.

Uses

Substitution

The most popular command of sed is the s command for substitution. To use the command, type an s, followed by a delimiting character, followed by a regular expression to look for, followed by the delimiting character, followed by an expression with which to replace what was found, followed by a final delimiting character. (/ is the conventional delimiting character and potentially required by some versions of sed. GNU sed can take any character however, and when URLs or paths are involved, % is often a handy choice to lessen the amount of escaping required.)

$ echo "Never let schooling interfere with your education." | sed 's/schooling/video games/'
Never let video games interfere with your education.

Groups can be useful in the replacement portion. Use the -r flag if you don't want to escape the initial grouping parenthesis. The -e flag allows for multiple expressions to be evaluated.

$ echo "Never let schooling interfere with your education." | sed -re 's/(schooling)/costly \1/' -e 's/(education)/invaluable \1/'
Never let costly schooling interfere with your invaluable education.

Examples