To prefix unique words with "UNIQUE:" inside a file I've tried to use a perl regex command like:
perl -e 'undef $/;while($_=<>){s/^(((?!\b\3\b).)*)\b(\w+)\b(((?!\b\3\b).)*)$/\1UNIQUE:\3\4/gs;print $_;}' demo
On a demo file containing:
watermelon banana
apple pear pineapple orange mango
strawberry cherry
kiwi pineapple lemon cranberry watermelon
orange plum cherry
kiwi banana plum
mango cranberry apple
lemon
The output is:
watermelon banana
apple pear pineapple orange mango
strawberry cherry
kiwi pineapple lemon cranberry watermelon
orange plum cherry
kiwi banana plum
mango cranberry apple
UNIQUE:lemon
Unfortunately, the \3 backreference doesn't seem to be handled if used in advance.
Is there another way to achieve this with another regex or with other usual commands available on a Linux box? (grep, sed, awk,...)
Many thanks
EDIT: Unfortunately, many of the solutions works for the provided case only which was incomplete, my apologies for that, it should also work on a text like:
{watermelon || banana}
apple = ( pear pineapple orange mango )
strawberry cherry
kiwi = pineapple = lemon = cranberry = watermelon
orange - plum = cherry
kiwi = banana + plum
mango = cranberry && apple
lemon
If it simplifies the problem, words may be prefixed with something like $ or @.