multiline find and replace regex perl

heres the string

#ssh.w /root/.ssh/gitlab.selectiont.com.wilmer
Host gitlab.selectiont.com 
PreferredAuthentications publickey 
IdentityFile /root/.ssh/gitlab.selectiont.com.wilmer 
#ssh.w /root/.ssh/gitlab.selectiont.com.wilmer

https://www.xmodulo.com/search-and-replace-multi-line-string.html

The -0 option turns Perl into “file slurp” mode, where Perl reads the entire input file in one shot (intead of line by line). This enables multi-line search and replace.

perl -i -0pe "s/\\s*#ssh.w{$key}(.*)\\n(.*)\\n(.*)\\n(.*)\\n(.*)#ssh.w{$key}(.*)\\n//" ~/.ssh/config

better version below

https://www.codegrepper.com/code-examples/whatever/regex+ignore+newline

perl -0pe "s/#ssh.w\\s$key[\s\S]*//" ~/.ssh/config

Great resources

https://jkorpela.fi/perl/regexp.html

Leave a Reply

Your email address will not be published.