I prefer to address this sort of thing by using a tool that can split the string using a multi-character delimiter. You can use your "excluded pattern" as the delimiter and then do replacements on elements which are not the delimiter. Because I like perl, I'll do a perl one-liner here. :)
First, because "perl" wasn't one of your suggested solutions, I'm guessing perl isn't something you're strong in. So I'll start with some things you need to know about perl to understand how this works:
If you put parens around the split pattern in the perl split
function, the separator is retained as an additional element in the array returned by split
. Using \[\[.*?\]\]
gets us the smallest string contained between [[
and ]]
, so in the returned array, we can select elements which don't start with [[
and do the replacement on only those elements. With foreach and map, $_ will be a reference (pointer) to the array element, so changes to $_ change the array elements. Thus, after changing the array, we can just join the potentially-modified elements and delimiters - still in the right order - back together with empty characters. Also, I like using unless()
while other people prefer if(!)
(same with my preference of using q{}
rather than ''
, because ''
looks kinda like "
and ""
looks like ''''
;)). This isn't code golf, and I think it's more readable my way. :)
Oh, and just in case this is also new: perl -lne
- the -l
transparently handles newlines, which I guess we really don't care about here, but it's habit. The -n
puts the code inside of a while(<>){}
.
With all that said, here's a working (but nonsensical) example replacing every non-link "a" with "pie":
danny@host [/home/danny]
$ cat testfile
a b c d [[a]] b c d [[ moo a moo]] a
I like to eat [[meat]] on a plate
danny@host [/home/danny]
$ perl -nle'@l=split(/(\[\[.*?\]\])/); foreach (@l){s/a/pie/g unless(/^\[\[/)};
print join(q{}, @l)' testfile
pie b c d [[a]] b c d [[ moo a moo]] pie
I like to epiet [[meat]] on pie plpiete
Is the exact term filling the entire space between the square brackets
[]
? – Wally – 2014-02-20T15:27:52.093No, it is not. There can be whitespaces or other charachters too. – BSDGuy – 2014-02-21T10:30:56.857
Thank yozu for your great Solution. I will try them and will report. – BSDGuy – 2014-02-21T10:32:13.957