I have a series of javascript .js files where the original scripter used poor variable names such as 'a', 'b', etc.
I need to automate the substitution to more descriptive names, reliably only changing the single character variable name(s) and not erroneously modifying other variables which may include that letter.
I am thinking it could be done in awk with something like:
awk '{gsub(/a/, "new_name")};{print}' script.js
but need more sophistication in the regular expression /a/ so as not to change variable names erroneously.
sed
orperl
as well... I think you are looking for word boundaries..sed 's/\ba\b/new_name/g' script.js
orperl -pe 's/\ba\b/new_name/g' script.js
if\b
isn't supported by yoursed
implementation – Sundeep Oct 02 '20 at 14:51a
froma
in the middle of a string or a comment or elsewhere. If you have specific code wherea
only appears in a specific context THEN you might be able to do what you want but we'd need to see that code to be able to help you. – Ed Morton Oct 02 '20 at 16:35(function(){var E;var g=window,n=document,p=function(a){var b=g._gaUserPrefs;if(b&&b.ioo&&b.ioo()||a&&!0===g["ga-disable-"+a])return!0;try{var c=g.external;if(c&&c._gaUserPrefs&&"oo"==c._gaUserPrefs)return!0}
– Debra McCusker Oct 02 '20 at 18:23I have created a file named charac-sub.awk with:
{gsub(/\/,"new_name1")}; {print}
{gsub(/\/,"new_name2")}; {print}
. .
and when I run it from the command line:
– Debra McCusker Oct 02 '20 at 20:29awk -f charac-sub.awk gs_script.js
It seems to do what I need.