My input
file has some text that needs to be changed. It has a couple of possibilities for example;
CONSTANT change/me !KEY2 !KEY3
CONSTANT change/me
CONSTANT change/me !DIFFERENTKEY
There is always a constant key (i.e. CONSTANT
) that doesn't change. There is also the possibilities of different keywords after the text that needs to be replaced. These are indicated with an exclamation mark (!
) and there may be no other keywords or thousands. I am only interested in replacing the text after CONSTANT
and any possible keys starting with !
. Preferably using sed
.
The following is my expected output. change/me
in input
has to be replaced with changed
;
CONSTANT changed !KEY2 !KEY3
CONSTANT changed
CONSTANT changed !DIFFERENTKEY
I have tried different things found in answers (here and here, here) on SO but I can't seem to figure it out. This closest I came was from this link.
sed -r 's/(^CONSTANT) (.+?) (\!.*$)/\1 changed \3/g' input
Which results in;
CONSTANT changed !KEY2
CONSTANT change/me
CONSTANT changed !DIFFERENTKEY
I tried different combinations but at this point it feels like I just shooting in the dark.
Conditions:
CONSTANT
doesn't changechange/me
is arbitrary text could also beblahblah
that needs to be replaced- A
!KEY
doesn't have to be present - A
!KEY
can be present and can be multiple of them, but always separated with space. I want to maintainCONSTANT
and any possible!KEY
s.