0

I need to replace a variable assignment statement using shell script. Eg: MyConstants.java file contains lines like

 String abcd = "ABCD"; 
 String abc = "ABC";
 String e = abcd + "e";

I need to replace variable abcd as

 String abcd = "newString"; 
 String abc = "ABC";
 String e = abcd + "e";

The problem is it contains \n \r characters in random manner like

  String abcd = 
 "ABCD"; 
 or 
 String abcd 
 = 

 "ABCD";
 String abc = "ABC";
 String e = abcd + "e";

I had a look at How to replace catch block in *.java using sed? But all commands are based on a single line pattern. (Takes input as a single line) How do I sort out this? Please help.

Community
  • 1
  • 1
LincyTonita
  • 327
  • 4
  • 14
  • I assume you need to replace the value of a variable giving the name of the variable, isn't it? – Alepac Mar 22 '13 at 10:55
  • @Alepac: Yes I need to replace value of the variable – LincyTonita Mar 22 '13 at 11:02
  • Do you need this to Patch an old BAD source code or do you need a kind of java Preprocessor? – Alepac Mar 22 '13 at 11:08
  • @Alepac: Actually we have a common project source. we need to update a java file that contains base url variable and generate different build for the same project source. – LincyTonita Mar 22 '13 at 11:19
  • you can youse properties or resources to load variable content that can change depending on the configuration isntead of hardcode some url to the code and change it in the compilation phase – Alepac Mar 22 '13 at 11:22
  • @Alepac: sry i don't know how to do that? Any links to have a look on? Anyway i need to do this by script also. If u have any ideas/solutions via script, pls let me know – LincyTonita Mar 22 '13 at 11:40
  • [This](http://docs.oracle.com/javase/1.4.2/docs/guide/lang/preferences.html) is a link to Java Preferences API – Alepac Mar 22 '13 at 11:49
  • I need a solution through script Alepac. Thanks for your valuable time spent.I gained some knowledge from this. If u have any solution reg script, pls let me know. – LincyTonita Mar 22 '13 at 12:22

3 Answers3

0

Why don't you replace just "ABCD" by "newString"? like

sed 's/\"ABCD\"/\"newString\"/'

Or /n/r symbols can be found in the middle of "ABCD"? Or, other way is to firstly remove /n/r/ which are placed not near ";"

0

Here is source file:

$ cat test
String abcd =
"ABCD";
String abcd
=
"ABCD";
String abc = "ABC";
String e = abcd + "e";

And this is, how we can replace all \n\r from wrong places and change variable value:

cat test | tr '\n' ' ' | sed 's/abcd = \"[a-zA-Z0-9_]*\"/abcd = \"newString\"/g' | tr ';' '\n'
String abcd = "newString"
 String abcd = "newString"
 String abc = "ABC"
 String e = abcd + "e"
0

See if this will work for you as a starting point.

sed -ne "H;/;/bsubst;\$bsubst;b;:subst;s/.*//;x;s/^\n//;/String\s*abcd/s/\".*\"/\"newstring\"/;p" testfile

Here's what it does: Every line is added to the hold space (H), then if it either contains a ; or is the last line, it jumps to the :subst label, otherwise it jumps to the end and goes to the next line.

At :subst we clear the current line and swap it with the hold space (this gets us the accumulated data and clears the hold space for the next batch). We then delete a newline character because there seems to be an extra one at this point (not sure why). After that it does the substitution of the variable's value if the line(s) contain the correct variable name and print whatever we have. I wouldn't say it's foolproof, but it works against the test data.

William
  • 4,787
  • 2
  • 15
  • 14
  • I'm getting following error. can u help `sed: 2: "H;/;/bsubst;$bsubst;b;: ...": undefined label 'subst;$bsubst;b;:subst;s/.*//;x;s/^\n//;/String\s*abcd/s/".*"/"newstring"/;p'` – LincyTonita Mar 26 '13 at 12:27
  • Works fine in my tests on rhel 6 and Cygwin, what OS are you using? – William Mar 26 '13 at 18:06
  • You could try adding some spaces - my versions of sed don't care, but... `/;/b subst; \$b subst; b; :subst;` Alternately, split out the label by itself: `"H;/;/bsubst;\$bsubst;b" -e ":subst" -e "s/.*/...` – William Mar 26 '13 at 18:12
  • Tried `sed -ne "H;/;/bsubst;\$bsubst;b" -e ":subst" -e "s/.*//;x;s/^\n//;/String\s*abcd/s/\".*\"/\"newstring\"/;p" test.txt` ... Got error as `sed: 2: "s/.*//;x;s/^\n//;/Strin ...": undefined label 'subst;$bsubst;b'` I'm working in MAC OS 10.8.2. Should I use any specific shell. i tried with default shell and also with bash. – LincyTonita Mar 27 '13 at 07:37
  • The shell won't make any difference, apparently the Mac OS sed is "different". This may help you: [Sed undefined label on macos](http://stackoverflow.com/questions/12272065/sed-undefined-label-on-macos) Note that we've already tried putting the label into it's own -e argument. – William Mar 27 '13 at 14:47