Sed script won't work with some elements

I have a file named messages.properties

that has syntax lines <key>=<string>

. Each key

is unique, but string

not. Many keys can have the same string. Also in the same folder I have some java classes that read lines from messages.properties

. They get the string using the method Messages.getString("<key>")

. So I need to convert Messages.getString("<key>")

to java classes in "<key>"

by reading their value from messages.properties

. Here's a 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 problem with this won't work for some in messages.properties

. How do I change my script to fix this problem? Here is a related question Search and replace with sed .

Example messages.properties

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

      

Java class example

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");

      

+3


source to share


1 answer


The actual problem was that eclipse split

Messages.getString("String.3")

      

in 2 lines,



    Messages
        .getString("String.3")

      

So it worked.

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

      

+1


source







All Articles