Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Last week I accidently externalized all my strings of my eclipse project. I need to revert this and my only hope is sed. I tried to create scripts but failed pathetically because I'm new with sed and this would be a very complicated operation. What I need to do is this:

Strings in class.java file is currently in the following format(method) Messages.getString(<key>). Example :

    if (new File(DataSource.DEFAULT_VS_PATH).exists()) {
        for (int i = 1; i <= c; i++) {
            if (!new File(DataSource.DEFAULT_VS_PATH
                    + Messages.getString("VSDataSource.89") + i).exists()) { //$NON-NLS-1$
                getnewvfspath = DataSource.DEFAULT_VS_PATH
                        + Messages.getString("VSDataSource.90") + i; //$NON-NLS-1$
                break;
            }
        }
    }

The key and matching Strings are in messages.properties file in the following format.

VSDataSource.92=No of rows in db = 
VSDataSource.93=Verifying db entry : 
VSDataSource.94=DB is open
VSDataSource.95=DB is closed
VSDataSource.96=Invalid db entry for 
VSDataSource.97=\ removed.
key=string

So I need the java file back in this format:

    if (new File(DataSource.DEFAULT_VS_PATH).exists()) {
        for (int i = 1; i <= c; i++) {
            if (!new File(DataSource.DEFAULT_VS_PATH
                    + "String 2" + i).exists()) { //$NON-NLS-1$
                getnewvfspath = DataSource.DEFAULT_VS_PATH
                        + "String 1" + i; //$NON-NLS-1$
                break;
            }
        }
    }

How can I accomplish this with sed? Or is there an easier way?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

The "trick" is to make a sed script out of the message.properties:

$ sed 's!/!\\!;s!\([^=]*\)=\(.*\)!s/"\1"/"\2"/g!' message.properties > message.sed
$ sed -f messages.sed class.java > class.java.reverted
$ rm messages.sed

You start with something like:

VSDataSource.92=No of rows in db = 
VSDataSource.93=Verifying db entry : 
VSDataSource.NN=Foo/bar

And end up with:

s/"VSDataSource.92"/"No of rows in db= "/g
s/"VSDataSource.93"/"Verifying db entry : "/g
s/"VSDataSource.NN"/"Foo\/bar"/g
share|improve this answer
add comment

No source control? tsk, tsk. I'd suggest fixing that problem before running this script that worked for me on a very small sample:

#!/bin/bash
set -x
for i in `seq 92 97`
do
    X=$(sed -n s/^VSDataSource.$i=//p messages.properties)
    sed -i "s+Messages.getString(\"VSDataSource.`echo $i`\")+\"`echo $X`\"+g" class.java
done

Of course, change the args to seq and sed (*java instead of class.java, perhaps). Seriously, though, there's no way this is the best solution to your problem. If it has an 'externalize strings' button, shouldn’t your IDE have an 'internalize strings' button?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.