Revision as of 08:01, 27 August 2002 editHirzel (talk | contribs)1,146 edits Added paragraph how to do several subsitutions← Previous edit | Revision as of 09:49, 29 August 2002 edit undoHirzel (talk | contribs)1,146 edits Changed the order of the two first examples and added a thirdNext edit → | ||
Line 1: | Line 1: | ||
]] | ]] | ||
'''Sed''' (which stands for '''S'''tream '''ED'''itor) is a ] utility which allows to modify text files. It reads |
'''Sed''' (which stands for '''S'''tream '''ED'''itor) is a originally a ] utility but is available for any operating system which supports a ]. It allows to modify text files. It reads input files line by line, transforms these lines according to rules specified in a certain simple language, and outputs them. The rules often involve ]s. | ||
In a typical unix pipe command, you might say something like | |||
⚫ | :generate_data | sed -e 's/x/y/' | ||
⚫ | That is, generate the data, ''but'' make the small change of replacing ''x'' by ''y''. | ||
This command is often used | |||
The following example shows a typical usage of sed: | |||
sed -e 's/OldExpression1/NewExpression/g' aFileName | sed -e 's/OldExpression1/NewExpression/g' aFileName | ||
Under Unix this can be combined with a pipe | |||
⚫ | :generate_data | sed -e 's/x/y/' | ||
⚫ | That is, generate the data, ''but'' make the small change of replacing ''x'' by ''y''. | ||
The ''s'' stands for substitute, the ''g'' stands for global. That means in the whole line. After the first slash there is the expression to search for and after the second slash there is the expression to substitute instead. The first expression can be a ]. | The ''s'' stands for substitute, the ''g'' stands for global. That means in the whole line. After the first slash there is the expression to search for and after the second slash there is the expression to substitute instead. The first expression can be a ]. | ||
Several substitutions can be put togehter in a file called for example ''subst.sed'' and then be applied like | |||
sed -f subst.sed inputFileName > outputFileName | |||
Besides substitution other forms of simple processing is possible. For example the following script deletes empty lines or lines which only contain spaces. | Besides substitution other forms of simple processing is possible. For example the following script deletes empty lines or lines which only contain spaces. | ||
sed -e '/^ *$/d' |
sed -e '/^ *$/d' inputFileName | ||
Revision as of 09:49, 29 August 2002
Sed (which stands for Stream EDitor) is a originally a UNIX utility but is available for any operating system which supports a command line. It allows to modify text files. It reads input files line by line, transforms these lines according to rules specified in a certain simple language, and outputs them. The rules often involve regular expressions.
The following example shows a typical usage of sed:
sed -e 's/OldExpression1/NewExpression/g' aFileName
Under Unix this can be combined with a pipe
- generate_data | sed -e 's/x/y/'
That is, generate the data, but make the small change of replacing x by y.
The s stands for substitute, the g stands for global. That means in the whole line. After the first slash there is the expression to search for and after the second slash there is the expression to substitute instead. The first expression can be a regular expression.
Several substitutions can be put togehter in a file called for example subst.sed and then be applied like
sed -f subst.sed inputFileName > outputFileName
Besides substitution other forms of simple processing is possible. For example the following script deletes empty lines or lines which only contain spaces.
sed -e '/^ *$/d' inputFileName
Sed is one of the very early unix commands that permitted command line processing of data files. 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.
If you have to do several substitutions you can put them in a file and use the following command:
sed -f subst.sed inputfileName > outputfileName
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.
Sed's language does not have variables and only primitive GOTO and branching functionality; nevertheless, the language is Turing complete.
Weblinks: