Replace each instance of a character in variable names across multiple files
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I have a list of template files that contain variables in this format:
my.var
my.other.var
I need to locate each instance and replace the .
with _
so that each variable becomes:
my_var
my_other_var
I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.
This is what I have so far:
for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;
Which results in
my_var
my.other_var
sed regular-expression
add a comment |Â
up vote
1
down vote
favorite
I have a list of template files that contain variables in this format:
my.var
my.other.var
I need to locate each instance and replace the .
with _
so that each variable becomes:
my_var
my_other_var
I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.
This is what I have so far:
for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;
Which results in
my_var
my.other_var
sed regular-expression
Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
â andcoz
Jul 18 at 14:06
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a list of template files that contain variables in this format:
my.var
my.other.var
I need to locate each instance and replace the .
with _
so that each variable becomes:
my_var
my_other_var
I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.
This is what I have so far:
for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;
Which results in
my_var
my.other_var
sed regular-expression
I have a list of template files that contain variables in this format:
my.var
my.other.var
I need to locate each instance and replace the .
with _
so that each variable becomes:
my_var
my_other_var
I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.
This is what I have so far:
for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;
Which results in
my_var
my.other_var
sed regular-expression
edited Jul 18 at 14:05
andcoz
11.5k32938
11.5k32938
asked Jul 18 at 13:51
Fantic
184
184
Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
â andcoz
Jul 18 at 14:06
add a comment |Â
Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
â andcoz
Jul 18 at 14:06
Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
â andcoz
Jul 18 at 14:06
Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
â andcoz
Jul 18 at 14:06
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
-1
down vote
accepted
Why not something like:
echo " my.other.var " | sed -n '/.*/s/./-/gp'
result:
my-other-var
Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .
Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
â Fantic
Jul 18 at 14:25
The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
â Fantic
Jul 18 at 14:47
echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
â ctrl-alt-delor
Jul 18 at 14:52
add a comment |Â
up vote
1
down vote
I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.
perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>
my.var becomes my_var
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
accepted
Why not something like:
echo " my.other.var " | sed -n '/.*/s/./-/gp'
result:
my-other-var
Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .
Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
â Fantic
Jul 18 at 14:25
The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
â Fantic
Jul 18 at 14:47
echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
â ctrl-alt-delor
Jul 18 at 14:52
add a comment |Â
up vote
-1
down vote
accepted
Why not something like:
echo " my.other.var " | sed -n '/.*/s/./-/gp'
result:
my-other-var
Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .
Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
â Fantic
Jul 18 at 14:25
The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
â Fantic
Jul 18 at 14:47
echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
â ctrl-alt-delor
Jul 18 at 14:52
add a comment |Â
up vote
-1
down vote
accepted
up vote
-1
down vote
accepted
Why not something like:
echo " my.other.var " | sed -n '/.*/s/./-/gp'
result:
my-other-var
Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .
Why not something like:
echo " my.other.var " | sed -n '/.*/s/./-/gp'
result:
my-other-var
Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .
answered Jul 18 at 14:11
Raman Sailopal
1,16317
1,16317
Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
â Fantic
Jul 18 at 14:25
The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
â Fantic
Jul 18 at 14:47
echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
â ctrl-alt-delor
Jul 18 at 14:52
add a comment |Â
Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
â Fantic
Jul 18 at 14:25
The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
â Fantic
Jul 18 at 14:47
echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
â ctrl-alt-delor
Jul 18 at 14:52
Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
â Fantic
Jul 18 at 14:25
Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
â Fantic
Jul 18 at 14:25
The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
â Fantic
Jul 18 at 14:47
The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
â Fantic
Jul 18 at 14:47
echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
â ctrl-alt-delor
Jul 18 at 14:52
echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
â ctrl-alt-delor
Jul 18 at 14:52
add a comment |Â
up vote
1
down vote
I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.
perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>
my.var becomes my_var
add a comment |Â
up vote
1
down vote
I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.
perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>
my.var becomes my_var
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.
perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>
my.var becomes my_var
I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.
perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>
my.var becomes my_var
answered Jul 18 at 14:37
Fantic
184
184
add a comment |Â
add a 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%2f456999%2freplace-each-instance-of-a-character-in-variable-names-across-multiple-files%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
Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
â andcoz
Jul 18 at 14:06