搜索并替换为 sed [英] Search and replace with sed

查看:33
本文介绍了搜索并替换为 sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我不小心外化了我的 eclipse 项目的所有字符串.我需要恢复这个,我唯一的希望是 sed.我尝试创建脚本但失败了,因为我是 sed 新手,这将是一个非常复杂的操作.我需要做的是:

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:

class.java 文件中的字符串目前采用以下格式(方法)Messages.getString().示例:

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;
            }
        }
    }

键和匹配的字符串在 messages.properties 文件中,格式如下.

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

所以我需要这个格式的 java 文件:

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;
            }
        }
    }

如何使用 sed 完成此操作?或者有更简单的方法吗?

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

推荐答案

这可能对你有用(GNU sed?):

This might work for you (GNU sed?):

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

这篇关于搜索并替换为 sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆