3

I have a file called messages.properties which has lines with syntax <key>=<string>. Each key is unique but string isn't. Many keys can have a same string. Also in the same folder I have some java classes which read the strings from messages.properties. They get string via the method Messages.getString("<key>"). So what I need to do is to convert Messages.getString("<key>") in java classes into "<key>" by reading their value from messages.properties. Here's the sed onliner from @potong that does the trick.

sed 's|^\([^=]*\)=\(.*\)|s@Messages.getString("\1")@"\2"@g|;s/\\/\\\\/g' messages.properties |
sed -i -f - *.java

However the issue with this is it won't work for some in messages.properties. How should I modify the script to solve this? Here's a related question Search and replace with sed .

Sample messages.properties

Sting.1=Str
Sting.2=String
Sting.3=String
Sting.4=Strring
Sting.5=Str

Sample java class

System.Out.println(Messages.getString("Sting.1"));
System.Out.println(Messages.getString("Sting.2"));
System.Out.println(Messages.getString("Sting.3"));
System.Out.println(Messages.getString("Sting.4"));
System.Out.println(Messages.getString("Sting.5"));

Reqired java class

System.Out.println("Str");
System.Out.println("String");
System.Out.println("String");
System.Out.println("Strring");
System.Out.println("Str");
Community
  • 1
  • 1
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
  • 2
    please consider updating your question with a sample properties file with 3 entries (or the bare minimum to illustrate the problem) and your required output from that same data. Good luck. – shellter Apr 05 '12 at 03:32
  • @shellter I will add it, but meanwhile you can check the link for samples. – Binoy Babu Apr 05 '12 at 03:34
  • hm... 2 things, don't see either VSDataSource.89 or VSDataSource.90 in your properties sample. 2. please show us the output you are getting for the above block. How do you mean 'won't work when the is repeated in messages.properties'? I guess I'm saying, it would be helpful to see exact inputs, exact and required outputs for same inputs that produce errors. Good luck. – shellter Apr 05 '12 at 03:41
  • @shellter edited the question accordingly – Binoy Babu Apr 05 '12 at 03:48
  • @potong Please help me with this. – Binoy Babu Apr 05 '12 at 03:50
  • I've never heard of that before :-! Just to help us small-timers out, please update your question to include those file sizes. Thanks and good luck. – shellter Apr 05 '12 at 04:14
  • @I was actally mistook, it was not a problem with sizes either. the actual problem was that eclipse split `Messages.getString("Sting.3")` into 2 lines, `Messages` and `.getString("Sting.3")`. So `sed 's|^\([^=]*\)=\(.*\)|s@.getString("\1")@"\2"@g|;s/\\/\\\\/g' messages.properties | sed -i -f - *.java` worked. – Binoy Babu Apr 05 '12 at 04:45

1 Answers1

1

The actual problem was that eclipse split

Messages.getString("String.3")

into 2 lines,

    Messages
        .getString("String.3")

So this worked.

sed 's|^\([^=]*\)=\(.*\)|s@.getString("\1")@"\2"@g|;s/\\/\\\\/g' messages.properties |
sed -i -f - *.java
Binoy Babu
  • 16,699
  • 17
  • 91
  • 134
  • Well done for spotting problem and fixing up the solution!. A word of caution though, beware of false positives the more you shorten the search string. – potong Apr 05 '12 at 05:39