Sed: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 46: | Line 46: | ||
== Regular expressions== |
== Regular expressions== |
||
See [[Regular Expressions]]. |
|||
The information below is only illustrative. See e.g. [[wikipedia:Regular_expression|Wikipedia page ]] for reference information. The list below is actually for ''extended regular expressions'', which can be obtained in ''sed'' using option '''-r''' (<tt>sed -r</tt>). |
|||
{| class="wikitable" |
|||
!Regexp!!Description |
|||
|- |
|||
|<tt>.</tt>||Match any character |
|||
|- |
|||
||<tt>gray|grey</tt>||Match ''gray'' or ''grey'' |
|||
|- |
|||
||<tt>gr(a|e)y</tt>||Match ''gray'' or ''grey'' |
|||
|- |
|||
||<tt>gr[ae]y</tt>||Match ''gray'' or ''grey'' |
|||
|- |
|||
||<tt>file[^0-2]</tt>||Match ''file3'' or ''file4'', but not ''file0'', ''file1'', ''file2''. |
|||
|- |
|||
|<tt>colou?r</tt>||''(zero or one)'' - Match ''Color'' or ''Colour''. |
|||
|- |
|||
|<tt>ab*c</tt>||''(zero or more)'' - Match ''ac'', ''abc'', ''abbc'', .... |
|||
|- |
|||
|<tt>ab+c</tt>||''(one or more)'' - Match ''abc'', ''abbc'', ''abbbc'', .... |
|||
|- |
|||
|<tt>a{3,5}</tt>||''(at least m and not more than n times)'' - Match ''aaa'', ''aaaa'', ''aaaaa''. |
|||
|- |
|||
|<tt>^on single line$</tt>||''(start and end of line)'' - Match ''on single line'' on a single line. |
|||
|} |
|||
<small>When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis '''( )''', or braces '''{ }''') must be quoted with '''backslash \'''.</small> |
|||
== Script Examples == |
== Script Examples == |
Revision as of 14:44, 25 November 2009
References
- The SED Homepage on SourceForge
- The SED FAQ
- The SED man page
- Sed, a stream editor
- The sed one-liners
Installation
It is recommended to add the following alias in your ~/.bashrc:
alias sed="sed -r"
Of course, this alias has no effect on shell script. There you'll have to specify the option explicitly at each invokation.
Usage
Some basic usage:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed -n # Silent - suppress automatic printing of pattern space
sed -r # Use extended regular expression
sed -i "s/foo/bar/" *.txt # In-place file modification
Use of address commands a\text, i\text, c\text. The command is terminated by a *newline*. To insert a newline character, use \n:
$ cat mytext
First line
Second line
$ cat mysedscript
1 {i\inserted text
s/$/ (not anymore)/g}
$ sed -f mysedscript mytext
inserted text
First line (not anymore)
Second line
# All on one line: use echo -e to generate the newline that terminates the command i\
$ echo -e "1 {i\\inserted text\ns/$/ (not anymore)/g}"| sed -f - mytext
inserted text
First line (not anymore)
Second line
#Same result without command \i:
$ sed "1 {s/^/inserted text\n/; s/$/ (not anymore)/}" mytext
Regular expressions
See Regular Expressions.
Script Examples
Remove <script>...</script> HTML tag
s!<script[>\x20\t].*</script>!!g
/<script[>\x20\t]/{
s!<script[>\x20\t].*!!g
:NEXTCYCLE
n
/<\/script>/!{
s!.*!!g
b NEXTCYCLE
}
s!.*</script>!!g
}