[Cialug] Regular Expression Search & Replace Problem

Zachary Kotlarek zach at kotlarek.com
Thu Jan 4 18:35:30 UTC 2018


On 3 Jan 2018, at 11:54, Todd Walton wrote:

> If I had a text file loaded in an editor with PCRE regular expression
> matching, how would I search & replace this:


You can’t do that in a line-by-line regex. You might be able to do it taking the whole set of lines into a single regex, but it wouldn’t be pretty.

Tools like awk or sed could make this work — they provide enough abstraction to use line-by-line labels while retaining data from previous labels — but I know perl better than awk so I’d do this to get a one-liner:
perl -e 'while (<STDIN>) { if (/^\s*(\w\w\.)\s*$/) { $sal = $1; } elsif (/^\s*$/) { print "\n"; } else { print $sal . $_; } }'

	Zach
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2483 bytes
Desc: S/MIME digital signature
URL: <http://cialug.org/pipermail/cialug/attachments/20180104/19bd00a5/attachment.bin>


More information about the Cialug mailing list