Sed
sed ("stream editor") is a Unix utility that parses and transforms text, using a simple, compact programming language. It was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs,[1] and is available today for most operating systems.[2] sed was based on the scripting features of the interactive editor ed ("editor", 1971) and the earlier qed ("quick editor", 1965–66). It was one of the earliest tools to support regular expressions, and remains in use for text processing, most notably with the substitution command. Popular alternative tools for plaintext string manipulation and "stream editing" include AWK and Perl. HistoryFirst appearing in Version 7 Unix,[3] sed is one of the early Unix commands built for command line processing of data files. It evolved as the natural successor to the popular grep command.[4] The original motivation was an analogue of grep (g/re/p) for substitution, hence "g/re/s".[3] Foreseeing that further special-purpose programs for each command would also arise, such as g/re/d, McMahon wrote a general-purpose line-oriented stream editor, which became sed.[4] The syntax for sed, notably the use of GNU sed added several new features, including in-place editing of files. Super-sed is an extended version of sed that includes regular expressions compatible with Perl. Another variant of sed is minised, originally reverse-engineered from 4.1BSD sed by Eric S. Raymond and currently maintained by René Rebe. minised was used by the GNU Project until the GNU Project wrote a new version of sed based on the new GNU regular expression library. The current minised contains some extensions to BSD sed but is not as feature-rich as GNU sed. Its advantage is that it is very fast and uses little memory. It is used on embedded systems and is the version of sed provided with Minix.[5] Mode of operationsed is a line-oriented text processing utility: it reads text, line by line, from an input stream or file, into an internal buffer called the pattern space. Each line read starts a cycle. To the pattern space, sed applies one or more operations which have been specified via a sed script. sed implements a programming language with about 25 commands that specify the operations on the text. For each input line, after running the script, sed ordinarily outputs the pattern space (the line as modified by the script) and begins the cycle again with the next line. Other end-of-script behaviors are available through sed options and script commands, e.g. The sed script can either be specified on the command line ( A main loop executes for each line of the input stream, evaluating the sed script on each line of the input. Lines of a sed script are each a pattern-action pair, indicating what pattern to match and which action to perform, which can be recast as a conditional statement. Because the main loop, working variables (pattern space and hold space), input and output streams, and default actions (copy line to pattern space, print pattern space) are implicit, it is possible to write terse one-liner programs. For example, the sed program given by: 10q will print the first 10 lines of input, then stop. UsageSubstitution commandThe following example shows a typical, and the most common, use of sed: substitution. This usage was indeed the original motivation for sed:[4] sed 's/regexp/replacement/g' inputFileName > outputFileName
In some versions of sed, the expression must be preceded by The substitution command, which originates in search-and-replace in ed, implements simple parsing and templating. The Other sed commandsBesides 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: sed '/^ *$/d' inputFileName
This example uses some of the following regular expression metacharacters (sed supports the full range of regular expressions):
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 (a colon followed by a string) and the branch instruction sed used as a filterUnder Unix, sed is often used as a filter in a pipeline: $ generateData | sed 's/x/y/g'
That is, a program such as "generateData" generates data, and then sed makes the small change of replacing x with y. For example: $ echo xyz xyz | sed 's/x/y/g'
yyz yyz
File-based sed scriptsIt is often useful to put several sed commands, one command per line, into a script file such as sed -f subst.sed inputFileName > outputFileName
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 line" containing the sed command and assigning the executable permission to the file. For example, a file #!/bin/sed -f
s/x/y/g
The file may then be made executable by the current user with the chmod u+x subst.sed
The file may then be executed directly from the command line: subst.sed inputFileName > outputFileName
In-place editingThe sed -i 's/abc/def/' fileName
ExamplesHello, world! example# convert input text stream to "Hello, world!"
s/.*/Hello, world!/
q
This "Hello, world!" script is in a file (e.g., script.txt) and invoked with The example emphasizes many key characteristics of sed:
Other simple examplesBelow follow various sed scripts; these can be executed by passing as an argument to sed, or put in a separate file and executed via To replace any instance of a certain word in a file with "REDACTED", such as an IRC password, and save the result: $ sed -i "s/yourpassword/REDACTED/" ./status.chat.log
To delete any line containing the word "yourword" (the address is '/yourword/'): /yourword/ d
To delete all instances of the word "yourword": s/yourword//g
To delete two words from a file simultaneously: s/firstword//g
s/secondword//g
To express the previous example on one line, such as when entering at the command line, one may join two commands via the semicolon: $ sed "s/firstword//g; s/secondword//g" inputFileName
Multiline processing exampleIn the next example, sed, which usually only works on one line, removes newlines from sentences where the second line starts with one space. Consider the following text: This is my dog, whose name is Frank. This is my fish, whose name is George. This is my goat, whose name is Adam. The sed script below will turn the text above into the following text. Note that the script affects only the input lines that start with a space: This is my dog, whose name is Frank. This is my fish, whose name is George. This is my goat, whose name is Adam. The script is: N
s/\n / /
P
D
This is explained as:
This can be expressed on a single line via semicolons: sed '
Limitations and alternativesWhile simple and limited, sed is sufficiently powerful for a large number of purposes. For more sophisticated processing, more powerful languages such as AWK or Perl are used instead. These are particularly used if transforming a line in a way more complicated than a regex extracting and template replacement, though arbitrarily complicated transforms are in principle possible by using the hold buffer. Conversely, for simpler operations, specialized Unix utilities such as grep (print lines matching a pattern), head (print the first part of a file), tail (print the last part of a file), and tr (translate or delete characters) are often preferable. For the specific tasks they are designed to carry out, such specialized utilities are usually simpler, clearer, and faster than a more general solution such as sed. The ed/sed commands and syntax continue to be used in descendent programs, such as the text editors vi and vim. An analog to ed/sed is sam/ssam, where sam is the Plan 9 editor, and ssam is a stream interface to it, yielding functionality similar to sed. See alsoNotes
References
Further reading
External linksWikibooks has a book on the topic of: Sed
Information related to Sed |