Gnu sed

From wikinotes
Revision as of 13:31, 20 September 2020 by Will (talk | contribs) (Created page with "Gnu sed performs search/replace within stdin or files.<br> See also bsd sed. = Usage = <blockquote> <source lang="bash"> # Replace 'aaa' with 'bbb' cat echo "aaa_stuff_aa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Gnu sed performs search/replace within stdin or files.
See also bsd sed.

Usage

# Replace 'aaa' with 'bbb'
cat echo "aaa_stuff_aaa" \
| sed 's_aaa_bbb_g'		


# Regex 
cat echo "aaa_stuff_aaa" \
| sed '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 's_\([a]*\)\(stuff\)_\2\n'


# Replace file contents
sed -i"bak"  "s_aaa_bbb_g" /path/to/file