- The correct title of this article is sed. The initial letter is shown capitalized due to technical restrictions.
sed (which stands for Stream EDitor) is a simple and powerful computer program used to apply various pre-specified textual transformations to a sequential stream of text data. It reads input files line by line, edits each line according to rules specified in its simple language (the sed script), and then outputs the line. While originally created as a Unix utility by Lee E. McMahon of Bell Labs from 1973 to 1974, sed is now available for virtually every operating system that supports a command line. A computer program is a collection of instructions that describe a task, or set of tasks, to be carried out by a computer. ...
Unix (officially trademarked as UNIX) is a computer operating system originally developed in the 1960s and 1970s by a group of AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie, and Douglas McIlroy. ...
Bell Laboratories (also known as Bell Labs and formerly known as AT&T Bell Laboratories and Bell Telephone Laboratories) was the main research and development arm of the United States Bell System. ...
A command line interface or CLI is a method of interacting with a computer by giving it lines of textual commands (that is, a sequence of characters) either from keyboard input or from a script. ...
Functions
sed is often thought of as a non-interactive text editor. It differs from conventional text editors in that the processing of the two inputs is inverted. Instead of iterating once through a list of edit commands applying each one to the whole text file in memory, sed iterates once through the text file applying the whole list of edit commands to each line. Because only one line at a time is in memory, sed can process text files with an arbitrarily-large number of lines. Some implementations of sed can only process lines of limited lengths. Notepad is the standard text editor for Microsoft Windows A text editor is a piece of computer software for editing plain text. ...
This article or section does not cite its references or sources. ...
sed's command set is modeled after the ed editor, and most commands work similarly in this inverted paradigm. For example, the command 25d means if this is line 25, then delete (don't output) it, rather than go to line 25 and delete it as it does in ed. The notable exceptions are the copy and move commands, which span a range of lines and thus don't have straightforward equivalents in sed. Instead, sed introduces an extra buffer called the hold space, and additional commands to manipulate it. The ed command to copy line 25 to line 76 (25t76) for example would be coded as two separate commands in sed (25h; 76g), to store the line in the hold space until the point at which it should be retrieved. The text editor ed was the original standard on the Unix operating system. ...
Usage The following example shows a typical usage of sed, where the -e option indicates that the sed expression follows: sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName The s stands for substitute; the g stands for global, which means that all matching occurrences in the line would be replaced. After the first slash is the regular expression to search for and after the second slash is the expression to replace it with. The substitute command (s///) is by far the most powerful and most commonly used sed command. In computing, a regular expression (abbreviated as regexp or regex, with plural forms regexps, regexes, or regexen) is a string that describes or matches a set of strings, according to certain syntax rules. ...
Under Unix, sed is often used as a filter in a pipeline: In UNIX and UNIX-like operating systems, a filter is program that gets most of its data from standard input and writes its main results to standard output. ...
generate_data | sed -e 's/x/y/g' That is, generate the data, but make the small change of replacing x with y. Several substitutions or other commands can be put together in a file called, for example, subst.sed and then be applied using the -f option to read the commands from the file: sed -f subst.sed inputFileName > outputFileName Besides substitution, other forms of simple processing are possible. For example, the following deletes empty lines or lines that only contain spaces: sed -e '/^ *$/d' inputFileName This example used some of the following regular expression metacharacters: A metacharacter is a character that has a general meaning instead of a literal meaning in a regular expression. ...
- The caret (
^) matches the beginning of the line. - The dollar sign (
$) matches the end of the line. - A period (
.) matches any single character. - The asterisk (
*) matches zero or more occurrences of the previous character. - A bracketed expression delimited by
[ and ] matches any of the characters inside the brackets. Complex sed constructs are possible, to the extent that it can be conceived of as a highly specialised, albeit simple, programming language. Flow of control, for example, can be managed by use of a label (a colon followed by a string which is to be the label name) and the branch instruction b; an instruction b followed by a valid label name will move processing to the block following the label; if the label does not exist then the branch will end the script. A caret in the Arial font Caret is the name for the symbol ^ in ASCII and some other character sets. ...
$ The dollar sign is a symbol primarily used to indicate a unit of currency. ...
A full stop or period (sometimes stop, full point or dot), is the punctuation mark commonly placed at the end of several different types of sentences in English and several other languages. ...
This article refers to the typographical symbol. ...
Various brackets in Arial // In writing Brackets are punctuation marks, used in pairs to set apart or interject text within other text. ...
A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. ...
History sed is one of the very early Unix commands that permitted command line processing of data files. It evolved as the natural successor to the popular grep command. Cousin to the later AWK, sed allowed powerful and interesting data processing to be done by shell scripts. sed was probably the earliest Unix tool that really encouraged regular expressions to be used ubiquitously. In terms of speed of operation, sed is generally faster than Perl in execution and markedly faster than AWK. grep is a command line utility originally written for use with the Unix operating system. ...
AWK is a general purpose computer language that is designed for processing text-based data, either in files or data streams. ...
sed and AWK are often cited as the progenitors and inspiration for Perl; in particular the s/// syntax from the example above is part of Perl's syntax. Perl is a dynamic programming language created by Larry Wall and first released in 1987. ...
sed's language does not have variables and has only primitive GOTO and branching functionality; nevertheless, the language is Turing-complete. [1] Goto may mean: GOTO (also known as Goto or Go to) â a branching construct in programming languages, infamous for its role in unstructured dialects of BASIC Goto, Nagasaki â a Japanese city G0-T0 (note: the characters following the G and T, respectively, are zeros), alias his coverup identity of Goto...
In computability theory a programming language or any other logical system is called Turing-complete if it has a computational power equivalent to a universal Turing machine. ...
GNU sed includes several new features such as in-place editing of files (i.e., replace the original file with the result of applying the sed program). In-place editing is often used instead of ed scripts: for example, GNU (pronounced ) is a free operating system consisting of a kernel, libraries, system utilities, compilers, and end-user applications. ...
The text editor ed was the original standard on the Unix operating system. ...
sed -i 's/abc/def/' file can be used instead of ed file 1,$ s/abc/def/ w q There is an extended version of sed called Super-sed (ssed) that includes regular expressions compatible with Perl. Perl is a dynamic programming language created by Larry Wall and first released in 1987. ...
Another version of sed is minised, originally reverse-engineered from the 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. Eric S. Raymond (FISL 6. ...
The GNU logo, drawn by Etienne Suvasa The GNU Project was announced in 1983 by Richard Stallman. ...
MINIX is an open source, Unix-like operating system (OS) based on a microkernel architecture. ...
Samples This example will enable sed, which usually only works on one line, to remove newlines from sentences where the second sentence starts with one space. Consider the following text: This is my cat my cat's name is betty This is my dog my dog's name is frank The sed script below will turn it into: This is my cat my cat's name is betty This is my dog my dog's name is frank Here's the script: sed 'N;s/n / /;P;D;' - (N) add the next line to the work buffer
- (s) substitute
- (/n /) match: n and one space
- (/ /) replace with: one space
- (P) print the top line of the work buffer
- (D) delete the top line from the work buffer and run the script again
The Address Command (submatches) More complex substitutions are possible using the "Address" command: /pattern1/s/pattern2/replacement/flags - will replace pattern2 with replacement where pattern1 is matched.
Likewise: /pattern1/!s/pattern2/replacement/flags - will replace pattern2 where pattern1 is *not* matched.
For example, if you have a file (text.txt) containing the following lines: Hello world. Hello world. I love sed. And you want to replace "world" with "mom", but only on those lines that contain the word "sed", you can use: sed -e '/sed/s/world/mom/g' text.txt will result in: Hello world. Hello mom. I love sed. You can negate this behavior with: sed -e '/sed/!s/world/mom/g' text.txt which will result in the opposite: Hello mom. Hello world. I love sed. Further reading OReilly Media (formerly OReilly & Associates, IPA /Éraɪli/) is an American media company established by Tim OReilly, primarily focusing on books related to computer programming. ...
1998 (MCMXCVIII) was a common year starting on Thursday of the Gregorian calendar, and was designated the International Year of the Ocean. ...
December 30 is the 364th day of the year (365th in leap years) in the Gregorian Calendar, with 1 day remaining. ...
Pearson can mean Pearson PLC the media conglomerate. ...
Linux refers to any Unix-like computer operating system which uses the Linux kernel. ...
The man page on man Almost all substantial UNIX and Unix-like operating systems have extensive documentation known as man pages (short for manual pages). The Unix command used to display them is man. ...
See also This is a list of Unix programs. ...
External links Wikibooks How to has more about this subject: sed |