Perl

Performing Regex Replacement for Multiline Text in Perl

Introducing how to perform regex replacement on multiline text data/text files with newlines using the Perl programming language from Linux command line Bash, PowerShell, or cmd. printf "hoge\nfuga\nfoo" | perl -pe...

Shou Arisaka
1 min read
Oct 3, 2025

Introducing how to perform regex replacement on multiline text data/text files with newlines using the Perl programming language from Linux command line Bash, PowerShell, or cmd.

ASUS:/mnt/c/pg$ printf "hoge\nfuga\nfoo" | perl -pe 's/.*fuga//igs'
hoge

ASUS:/mnt/c/pg$ printf "hoge\nfuga\nfoo" | perl -pe 'BEGIN{undef $/;} s/.*fuga//smg'

foo

With Ruby, you can just specify options…

When passing standard input from Bash for multiline replacement, sed won’t work, Python doesn’t seem to work either, so what remains is this Perl and probably Ruby can do it too. Maybe PHP can as well. I used Perl this time, but I think the code could be shorter with Ruby.

Multiline search replace with Perl - Stack Overflow

Share this article

Shou Arisaka Oct 3, 2025

🔗 Copy Links