Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
2 added clarification about word boundaries
source | link

I'm using awk '{ gsub(/BAR|WIBBLE/, "FOO"); print }' to replace text in data like:

SOMETHING [BAR, WIBBLE]
SOMETHING [BAR]

This gives the desired result of:

SOMETHING [FOO, FOO]
SOMETHING [FOO]

But now I've had to update the text that requires replacing to be something like:

awk '{ gsub(/BAR|WIBBLE|ME/, "FOO"); print }'

Which turns text like:

SOMETHING [ME, WIBBLE]

into:

SOFOOTHING [FOO, FOO]

How can I limit my replacement to just the text between the brackets (i.e. leave the SOMETHING alone)?

EDIT

I also need robustness across whatever text SOMETHING might be (e.g. SHE GAVE ME THAT shouldn't have ME replaced).

I'm using awk '{ gsub(/BAR|WIBBLE/, "FOO"); print }' to replace text in data like:

SOMETHING [BAR, WIBBLE]
SOMETHING [BAR]

This gives the desired result of:

SOMETHING [FOO, FOO]
SOMETHING [FOO]

But now I've had to update the text that requires replacing to be something like:

awk '{ gsub(/BAR|WIBBLE|ME/, "FOO"); print }'

Which turns text like:

SOMETHING [ME, WIBBLE]

into:

SOFOOTHING [FOO, FOO]

How can I limit my replacement to just the text between the brackets (i.e. leave the SOMETHING alone)?

I'm using awk '{ gsub(/BAR|WIBBLE/, "FOO"); print }' to replace text in data like:

SOMETHING [BAR, WIBBLE]
SOMETHING [BAR]

This gives the desired result of:

SOMETHING [FOO, FOO]
SOMETHING [FOO]

But now I've had to update the text that requires replacing to be something like:

awk '{ gsub(/BAR|WIBBLE|ME/, "FOO"); print }'

Which turns text like:

SOMETHING [ME, WIBBLE]

into:

SOFOOTHING [FOO, FOO]

How can I limit my replacement to just the text between the brackets (i.e. leave the SOMETHING alone)?

EDIT

I also need robustness across whatever text SOMETHING might be (e.g. SHE GAVE ME THAT shouldn't have ME replaced).

1
source | link

Replace text between brackets

I'm using awk '{ gsub(/BAR|WIBBLE/, "FOO"); print }' to replace text in data like:

SOMETHING [BAR, WIBBLE]
SOMETHING [BAR]

This gives the desired result of:

SOMETHING [FOO, FOO]
SOMETHING [FOO]

But now I've had to update the text that requires replacing to be something like:

awk '{ gsub(/BAR|WIBBLE|ME/, "FOO"); print }'

Which turns text like:

SOMETHING [ME, WIBBLE]

into:

SOFOOTHING [FOO, FOO]

How can I limit my replacement to just the text between the brackets (i.e. leave the SOMETHING alone)?