Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Sed
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Usage== ===Substitution command=== The following example shows a typical, and the most common, use of sed: substitution. This usage was indeed the original motivation for sed:<ref name=early_history /> <syntaxhighlight lang=bash> sed 's/regexp/replacement/g' inputFileName > outputFileName </syntaxhighlight> In some versions of sed, the expression must be preceded by <code>-e</code> to indicate that an expression follows. The <code>s</code> stands for substitute, while the <code>g</code> stands for global, which means that all matching occurrences in the line would be replaced. The [[regular expression]] (i.e. pattern) to be searched is placed after the first delimiting symbol (slash here) and the replacement follows the second symbol. Slash (<code>/</code>) is the conventional symbol, originating in the character for "search" in ed, but any other could be used to make syntax more readable if it does not occur in the pattern or replacement; this is useful to avoid "[[leaning toothpick syndrome]]". The substitution command, which originates in search-and-replace in ed, implements simple parsing and [[templating language|templating]]. The <code>regexp</code> provides both pattern matching and saving text via sub-expressions, while the <code>replacement</code> can be either literal text, or a format string containing the characters <code>&</code> for "entire match" or the special [[escape sequence]]s <code>\1</code> through <code>\9</code> for the ''n''th saved sub-expression. For example, <code>sed -r "s/(cat|dog)s?/\1s/g"</code> replaces all occurrences of "cat" or "dog" with "cats" or "dogs", without duplicating an existing "s": <code>(cat|dog)</code> is the 1st (and only) saved sub-expression in the regexp, and <code>\1</code> in the format string substitutes this into the output. ===Other sed commands=== Besides substitution, other forms of simple processing are possible, using some 25 sed commands. For example, the following uses the ''d'' command to filter out lines that only contain spaces, or only contain the end of line character: <syntaxhighlight lang=bash> sed '/^ *$/d' inputFileName </syntaxhighlight> This example uses some of the following [[regular expression]] [[metacharacter]]s (sed supports the full range of regular expressions): * The [[caret]] (<code>^</code>) matches the beginning of the line. * The [[dollar sign]] (<code>$</code>) matches the end of the line. * The [[asterisk]] (<code>*</code>) matches zero or more occurrences of the previous character. * The [[Plus and minus signs#Plus sign|plus]] (<code>+</code>) matches one or more occurrence(s) of the previous character. * The [[question mark]] (<code>?</code>) matches zero or one occurrence of the previous character. * The [[Full stop|dot]] (<code>.</code>) matches exactly one character. Complex sed constructs are possible, allowing it to serve as a simple, but highly specialized, [[programming language]]. Flow of control, for example, can be managed by the use of a [[Label (programming language)|label]] (a colon followed by a string) and the branch instruction <code>b</code>, as well as the conditional branch <code>t</code>. An instruction <code>b</code> followed by a valid label name will move processing to the command following that label. The <code>t</code> instruction will only do so if there was a successful substitution since the previous <code>t</code> (or the start of the program, in case of the first <code>t</code> encountered). Additionally, the <code>{</code> instruction starts a [[Block (programming)|subsequence]] of commands (up to the matching <code>}</code>); in most cases, it will be conditioned by an address pattern. ===sed used as a filter=== Under Unix, sed is often used as a [[filter (Unix)|filter]] in a [[pipeline (Unix)|pipeline]]: <syntaxhighlight lang="console"> $ generateData | sed 's/x/y/g' </syntaxhighlight> That is, a program such as "generateData" generates data, and then sed makes the small change of replacing ''x'' with ''y''. For example: <syntaxhighlight lang="console"> $ echo xyz xyz | sed 's/x/y/g' yyz yyz </syntaxhighlight> <ref group="notes" name="quotes"> In command line use, the quotes around the expression are not required, and are only necessary if the shell would otherwise not interpret the expression as a single word (token). For the script <code>s/x/y/g</code> there is no ambiguity, so <code>generateData | sed s/x/y/g</code> works correctly. However, quotes are usually included for clarity, and are often necessary, notably for whitespace (e.g., <code>'s/x x/y y/'</code>). Most often single quotes are used, to avoid having the shell interpret <code>$</code> as a shell variable. Double quotes are used, such as <code>"s/$1/$2/g"</code>, to allow the shell to substitute for a command line argument or other shell variable. </ref> ===File-based sed scripts=== It is often useful to put several sed commands, one command per line, into a script file such as <code>subst.sed</code>, and then use the <code>-f</code> option to run the commands (such as <code>s/x/y/g</code>) from the file: <syntaxhighlight lang=bash> sed -f subst.sed inputFileName > outputFileName </syntaxhighlight> Any number of commands may be placed into the script file, and using a script file also avoids problems with shell escaping or substitutions. Such a script file may be made directly executable from the command line by prepending it with a "[[Shebang (Unix)|shebang]] line" containing the sed command and assigning the executable permission to the file. For example, a file <code>subst.sed</code> can be created with contents: <syntaxhighlight lang="sed"> #!/bin/sed -f s/x/y/g </syntaxhighlight> The file may then be made executable by the current user with the <code>chmod</code> command:<syntaxhighlight lang=bash> chmod u+x subst.sed </syntaxhighlight>The file may then be executed directly from the command line: <syntaxhighlight lang=bash> subst.sed inputFileName > outputFileName </syntaxhighlight> ===In-place editing=== The <code>-i</code> option, introduced in GNU sed, allows in-place editing of files (actually, a temporary output file is created in the background, and then the original file is replaced by the temporary file). For example: <syntaxhighlight lang=bash> sed -i 's/abc/def/' fileName </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Sed
(section)
Add topic