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");
source to share