Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Point 2 is moot: You can put the Sed script in a file and then the shell doesn't get in your way, all... (4 comments)

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

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »