Sed: Difference between revisions

From miki
Jump to navigation Jump to search
(New page: == References == * [http://sed.sourceforge.net/ The SED Homepage on SourceForge] * [http://sed.sourceforge.net/sedfaq.html The SED FAQ] * [http://www.opengroup.org/onlinepubs/007908799/xcu...)
 
No edit summary
Line 5: Line 5:
* [http://www.gnu.org/software/sed/manual/sed.html Sed, a stream editor]
* [http://www.gnu.org/software/sed/manual/sed.html Sed, a stream editor]
* [http://sed.sourceforge.net/sed1line.txt The sed one-liners]
* [http://sed.sourceforge.net/sed1line.txt The sed one-liners]

== Installation ==
It is recommended to add the following alias in your <tt>~/.bashrc</tt>:
<source lang="bash">
alias sed="sed -r"
</source>
Of course, this alias has no effect on shell script. There you'll have to specify the option explicitly at each invokation.


== Usage ==
== Usage ==
Line 64: Line 71:
|}
|}
<small>When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis '''( )''', or braces '''{ }''') must be quoted with '''backslash \'''.</small>
<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 ==
=== Remove <script>...</script> HTML tag ===
<source lang="text">
s!<script[>\x20\t].*</script>!!g
/<script[>\x20\t]/{
s!<script[>\x20\t].*!!g
:NEXTCYCLE
n
/<\/script>/!{
s!.*!!g
b NEXTCYCLE
}
s!.*</script>!!g
}
</source>

Revision as of 09:10, 19 January 2009

References

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

The information below is only illustrative. See e.g. Wikipedia page for reference information. The list below is actually for extended regular expressions, which can be obtained in sed using option -r (sed -r).

Regexp Description
. Match any character
gray|grey Match gray or grey
gr(a|e)y Match gray or grey
gr[ae]y Match gray or grey
file[^0-2] Match file3 or file4, but not file0, file1, file2.
colou?r (zero or one) - Match Color or Colour.
ab*c (zero or more) - Match ac, abc, abbc, ....
ab+c (one or more) - Match abc, abbc, abbbc, ....
a{3,5} (at least m and not more than n times) - Match aaa, aaaa, aaaaa.
^on single line$ (start and end of line) - Match on single line on a single line.

When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis ( ), or braces { }) must be quoted with backslash \.

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
}