Gnu sed: Difference between revisions
From wikinotes
(→Usage) |
|||
Line 4: | Line 4: | ||
= Overview = | = Overview = | ||
<blockquote> | <blockquote> | ||
sed lets you manipulate text streams using commands. | sed lets you manipulate text streams using commands.<br> | ||
example commands: | |||
* <code>s</code> command search/replaces text (by default on each line)(ex: <code>sed 's/ | * <code>s</code> command search/replaces text (by default on each line)(ex: <code>echo 'foo_bar_baz' | sed 's/^foo/FOO/'</code>) | ||
* <code>i</code> and <code>a</code> prepend/append lines to the stream (ex: <code>echo 'foo_bar_baz' | sed 'i \above stream'</code>) | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> |
Revision as of 13:42, 16 October 2021
Gnu sed performs search/replace within stdin or files.
See also bsd sed.
Overview
sed lets you manipulate text streams using commands.
example commands:
s
command search/replaces text (by default on each line)(ex:echo 'foo_bar_baz' | sed 's/^foo/FOO/'
)i
anda
prepend/append lines to the stream (ex:echo 'foo_bar_baz' | sed 'i \above stream'
)
Usage
# Replace 'aaa' with 'bbb' cat echo "aaa_stuff_aaa" \ | sed 's_aaa_bbb_g' # Regex cat echo "aaa_stuff_aaa" \ | sed -E 's_[a]*_b_g' # Group matching (\2 is a variable that refers to the # second match group. In this case, 'stuff' cat echo "aaa_stuff_aaa" \ | sed -E 's_\([a]*\)\(stuff\)_\2\n' # Replace file contents sed -i"bak" "s_aaa_bbb_g" /path/to/file