0

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.

  • 2
    For those of us who don't speak JS, it might be helpful to include a description of the variable naming rules. For example, if names may only include word characters, then it might be possible to restrict the match using word boundary anchors. – steeldriver Oct 02 '20 at 14:16
  • you can use `sed` or `perl` as well... I think you are looking for word boundaries.. `sed 's/\ba\b/new_name/g' script.js` or `perl -pe 's/\ba\b/new_name/g' script.js` if `\b` isn't supported by your `sed` implementation – Sundeep Oct 02 '20 at 14:51
  • See also: [Replacing pattern only when it matches a whole word](https://unix.stackexchange.com/questions/356183/replacing-pattern-only-when-it-matches-a-whole-word) – Sundeep Oct 02 '20 at 14:52
  • 1
    Assuming JS includes the ability to enter text strings, comments, etc, would think some syntactic analysis would be very good idea. – Paul_Pedant Oct 02 '20 at 16:00
  • 1
    In general it's impossible to do this task robustly with a regexp since it can't tell a variable `a` from `a` in the middle of a string or a comment or elsewhere. If you have specific code where `a` 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
  • You have to use some tool written in js and specific for parsing js code. Like [this](https://stackoverflow.com/questions/53112448/how-to-replace-variable-names-in-external-js-files) and I am sure you will find more with a good search. These code parsing tools should also validate the code after any change. – thanasisp Oct 02 '20 at 17:16
  • Here is a portion of the .js file. I am not a javascript programmer so I cannot comment on the functionality. The "problem" is to perform the variable substitution as I mention above. ---- (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:23
  • Well folks, I believe that I have a solution; at least it is close.

    I 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:

    awk -f charac-sub.awk gs_script.js

    It seems to do what I need.
    – Debra McCusker Oct 02 '20 at 20:29
  • Don't post additional information in comments where it can't be formatted and might be missed, [edit] your question to include all relevant information. – Ed Morton Oct 03 '20 at 20:42

1 Answers1

0

Well folks, I believe that I have a solution; at least it is close.

I have created a file named charac-sub.awk with:
{gsub(/\<a>/,"new_name1")
gsub(/\<b>/,"new_name2")
.
.
gsub(/\<e>/,"new_nameN")}; {print}


and when I run it from the command line:

awk -f charac-sub.awk script.js

It seems to do what I need.