Day 56: mastering sed with regexps
For a basic use of sed, please read the 2 previous posts ;)
$ cat foobar.txt
foo
bar
$ cat foobar.txt | sed -e 's/o$/f/'
fof
bar
$ cat foobar.txt | sed -e 's/^/- /g'
- foo
- bar
$ cat foobar.txt | sed -e 's#\(a\|o\)##g'
f
br
Variables
You can extract each part of regexp and inject it with \1, \2, \x…
(Starting at 1, not 0…)
$ cat foobar.txt
foo
bar
# Inject space between characters and reverse 2 first letters:
$ cat foobar.txt | sed 's#\(.\)\(.\)\(.\)#\2 \1 \3#g'
o f o
a b r
Do not forget backslashes !! (\).
:bulb: When playing with sed and escaping characters with \, I often use # as separator to save my eyes ;)
by ops for non-ops