I have some texts in the file looking like:
'PALAL_3002_NEP_POLICIES':
Name: SomeRandomName:Here
Desc: "Random description here"
Chart Name: palal
Labels:
- Label1: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY'
- Label2: '#AL_RAND_DUMMY_SER_PSA_NEP_POLICY-DUMMY_SER_PAL_NEP'
- Label3: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY-DUMMY_SER_NEG_NEP'
- Label4: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY-#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY'
I want to find a word containing a string PAL
in capital letters and suffix the matched word with _MATCH
. The string PAL
can exist in a word without hyphen , or with hyphen separated.
Expected output:
'PALAL_3002_NEP_POLICIES_MATCH':
Name: SomeRandomName:Here
Desc: "Random description here"
Chart Name: palal
Labels:
- Label1: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY_MATCH'
- Label2: '#AL_RAND_DUMMY_SER_PSA_NEP_POLICY-DUMMY_SER_PAL_NEP_MATCH'
- Label3: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY_MATCH-DUMMY_SER_NEG_NEP'
- Label4: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY_MATCH-#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY_MATCH'
This is what I did but it suffixed every words with _MATCH
:
sed '/PAL/s/\>/_MATCH/g' practice.txt
Any help would be appreciated.
CodePudding user response:
If the text you want to append to only contains uppercase characters, digits and underscores, you can use the following:
sed 's/\(PAL[A-Z_0-9]*\)/\1_MATCH/g' foo
\( \) #capturing group
PAL #literal text
[A-Z_0-9] #character class, any of the contained characters
* #quantifier, match previous expression between 1 to infinity times
\1 #backreference to the first capturing group
_MATCH #literal text, suffix
Example:
╰─$ sed 's/\(PAL[A-Z_0-9]*\)/\1_MATCH/g' foo
'PALAL_3002_NEP_POLICIES_MATCH':
Name: SomeRandomName:Here
Desc: "Random description here"
Chart Name: palal
Labels:
- Label: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY_MATCH'
CodePudding user response:
Using sed
$ sed "/PAL/s/\(.*\)\('\)/\1_MATCH\2/" input_file
'PALAL_3002_NEP_POLICIES_MATCH':
Name: SomeRandomName:Here
Desc: "Random description here"
Chart Name: palal
Labels:
- Label: '#AL_RAND_DUMMY_PAL_PSA_NEP_POLICY_MATCH'