0

My file contains

$Param_T=ABC49_SA_T2
$Param_V=ABC49BC_SA_V2
$Param_ST=ABC491_SA_M2

I am trying to replace a value starts with ABC*_ with XYZ12_

sed -i 's/ABC\.*_/XYZ12_/g' INCR.parm

Above is the sed command i have used. It is not throwing any error but value is not changing.Could someone please help me with this ?

1

With sed, the way to implement non-greedy matching is to specify a set of character you don't want to match:

sed -i 's/=ABC[^_]*_/=XYZ12_/g' INCR.parm

That is: match "ABC" followed by zero or more non-underscore characters followed by underscore.

And note, you don't escape the dot. A "bare" dot means "match any character"; an escaped dot means "match a literal dot". You weren't making any replacements because none of the data matches the literal string "ABC."

  • 1
    You might want to match the = as well, or the start of the word ABC in some other way (\<). Otherwise they will do weird things to lines like ...=BOOABC09_.... – Kusalananda Jun 28 '18 at 13:20
0

Dont escape the "." that matches all characters [ Greedy]

sed -i 's/ABC.*_/XYZ12_/g' INCR.parm

Output:

$Param_T=XYZ12_T2
$Param_V=XYZ12_V2
$Param_ST=XYZ12_M2)

If you want to replace ABC* only uptill first underscore ( _ ) [Lazy], you need to use perl regex

use

perl -pe  's/ABC(.*?)_/XYZ12_/g' INCR.parm

Output:

$Param_T=XYZ12_SA_T2
$Param_V=XYZ12_SA_V2
$Param_ST=XYZ12_T2

'Greedy' means match longest possible string.

'Lazy' means match shortest possible string.

  • Thanks for this..It worked. But have some issue when there is a tab or space before $Param* (sample " $Param_T=ABC12_T2") . Value is not getting replaced. – user_297020 Jun 28 '18 at 11:02
  • I dont think so it should be a problem, can you update the question with the exact values you need to get replaced , i tried with your sample and its working for me – Arushix Jun 28 '18 at 11:18
  • " $Param_STG_V=DTST49_SA_STG_RKEM2_V" " $Param_SIM_T=DTST49_SA_SIM_RKEM2" " $Param_SIM_V=DTST49_SA_SIM_RKEM2_V" " $Param_WRK_T=DTST49_TMP_LOAD_UTILITY" Above is my sample data, I have to replace with DTST49 with DDEV25, – user_297020 Jun 28 '18 at 11:23
  • have added quotes as it is not accepting space or tab before – user_297020 Jun 28 '18 at 11:24
  • urplease edit the question and add your sample, as it is not very clear – Arushix Jun 28 '18 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.