Substituting text in a sed like manner but with a richer format
+1
−4
I have the following problems working with sed
:
- It doesn't allow multiline operations (thus, no indentations, no nesting)
- It is generally obligatory to wrap entire commands in quote marks (
sed "SED_COMMAND" FILE
) even if the command itself contains quote marks.
Without these problems I could format a long liner such as sed -i "s/\$to = ".*";$/\$to = example@example.com;/g" PATH
as in this pseudocode:
sed -i
A
\$to = ".*";$
B
\$to = example@example.com;
G
PATH
- A means "from"
- B means "to"
- G means "global"
Since sed
doesn't work this way, how could I achieve a similar syntax in the shell? Perhaps by using Python? Perl? Something else?
1 answer
+2
−0
Perl 5's /x
modifier ignores whitespace in the regex, but it doesn't seem to ignore whitespace in the replacement (and I don't know enough perl to fix this). However, if you want to match whitespace, it will have to be escaped. See the perlre§/x and /xx.
perl -pe '
s/
\$to\ =\ ".*";$
/\$to = example\@example.com;
/xg
' PATH
1 comment thread