sed replace issue

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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 ?
shell-script sed
add a comment |Â
up vote
0
down vote
favorite
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 ?
shell-script sed
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 ?
shell-script sed
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 ?
shell-script sed
edited Jun 28 at 13:16
glenn jackman
45.6k264100
45.6k264100
asked Jun 28 at 10:45
user_297020
116
116
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
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 wordABCin some other way (<). Otherwise they will do weird things to lines like...=BOOABC09_....
â Kusalananda
Jun 28 at 13:20
Good point, thanks
â glenn jackman
Jun 28 at 13:26
add a comment |Â
up vote
0
down vote
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 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 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 at 11:23
have added quotes as it is not accepting space or tab before
â user_297020
Jun 28 at 11:24
urplease edit the question and add your sample, as it is not very clear
â Arushix
Jun 28 at 12:05
 |Â
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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 wordABCin some other way (<). Otherwise they will do weird things to lines like...=BOOABC09_....
â Kusalananda
Jun 28 at 13:20
Good point, thanks
â glenn jackman
Jun 28 at 13:26
add a comment |Â
up vote
1
down vote
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 wordABCin some other way (<). Otherwise they will do weird things to lines like...=BOOABC09_....
â Kusalananda
Jun 28 at 13:20
Good point, thanks
â glenn jackman
Jun 28 at 13:26
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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."
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."
edited Jun 28 at 13:26
answered Jun 28 at 13:15
glenn jackman
45.6k264100
45.6k264100
1
You might want to match the=as well, or the start of the wordABCin some other way (<). Otherwise they will do weird things to lines like...=BOOABC09_....
â Kusalananda
Jun 28 at 13:20
Good point, thanks
â glenn jackman
Jun 28 at 13:26
add a comment |Â
1
You might want to match the=as well, or the start of the wordABCin some other way (<). Otherwise they will do weird things to lines like...=BOOABC09_....
â Kusalananda
Jun 28 at 13:20
Good point, thanks
â glenn jackman
Jun 28 at 13:26
1
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 at 13:20
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 at 13:20
Good point, thanks
â glenn jackman
Jun 28 at 13:26
Good point, thanks
â glenn jackman
Jun 28 at 13:26
add a comment |Â
up vote
0
down vote
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 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 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 at 11:23
have added quotes as it is not accepting space or tab before
â user_297020
Jun 28 at 11:24
urplease edit the question and add your sample, as it is not very clear
â Arushix
Jun 28 at 12:05
 |Â
show 1 more comment
up vote
0
down vote
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 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 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 at 11:23
have added quotes as it is not accepting space or tab before
â user_297020
Jun 28 at 11:24
urplease edit the question and add your sample, as it is not very clear
â Arushix
Jun 28 at 12:05
 |Â
show 1 more comment
up vote
0
down vote
up vote
0
down vote
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.
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.
edited Jun 28 at 11:16
answered Jun 28 at 10:50
Arushix
9968
9968
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 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 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 at 11:23
have added quotes as it is not accepting space or tab before
â user_297020
Jun 28 at 11:24
urplease edit the question and add your sample, as it is not very clear
â Arushix
Jun 28 at 12:05
 |Â
show 1 more comment
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 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 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 at 11:23
have added quotes as it is not accepting space or tab before
â user_297020
Jun 28 at 11:24
urplease edit the question and add your sample, as it is not very clear
â Arushix
Jun 28 at 12:05
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 at 11:02
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 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 at 11:18
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 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 at 11:23
" $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 at 11:23
have added quotes as it is not accepting space or tab before
â user_297020
Jun 28 at 11:24
have added quotes as it is not accepting space or tab before
â user_297020
Jun 28 at 11:24
urplease edit the question and add your sample, as it is not very clear
â Arushix
Jun 28 at 12:05
urplease edit the question and add your sample, as it is not very clear
â Arushix
Jun 28 at 12:05
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452405%2fsed-replace-issue%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password