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