Home > Archive > AWK > July 2006 > changing specific commented lines from java code ...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
changing specific commented lines from java code ...
|
|
| hector03@gmail.com 2006-07-10, 3:56 am |
| I have to replace four lines in a java code to modify it and then
replace back all the changes done.
CODE:
public static String res_data =
ResourceManager.getResourcePath("pathway.nlp:data");
// public final static String res_data = "resources/data/";
public final static String datadir = (res_data==null) ?
"resources" + File.separator + "data" + File.separator : res_data +
File.separator;
// public final static String datadir = "resources/data/";
i have to comment the uncommented lines in this code and uncomment the
commented lines too. Also i have to replace back the changes done after
i run the code.
The result that i am expecting from my awk script is :
// public static String res_data =
ResourceManager.getResourcePath("pathway.nlp:data");
public final static String res_data = "resources/data/";
// public final static String datadir = (res_data==null) ?
"resources" + File.separator + "data" + File.separator : res_data +
File.separator;
public final static String datadir = "resources/data/";
could some one help me in this regard .. by providing some short way to
do this.
I am currently searching for the string pattern say for example : "
public static String res_data"
and then appending "//" to $0(the whole line)..is there some other way
to do it.
Thanks .
| |
| hector03@gmail.com 2006-07-10, 7:56 am |
|
hector03@gmail.com wrote:
> I have to replace four lines in a java code to modify it and then
> replace back all the changes done.
>
> CODE:
> public static String res_data =
> ResourceManager.getResourcePath("pathway.nlp:data");
> // public final static String res_data = "resources/data/";
>
> public final static String datadir = (res_data==null) ?
> "resources" + File.separator + "data" + File.separator : res_data +
> File.separator;
> // public final static String datadir = "resources/data/";
>
> i have to comment the uncommented lines in this code and uncomment the
> commented lines too. Also i have to replace back the changes done after
> i run the code.
> The result that i am expecting from my awk script is :
> // public static String res_data =
> ResourceManager.getResourcePath("pathway.nlp:data");
> public final static String res_data = "resources/data/";
>
> // public final static String datadir = (res_data==null) ?
> "resources" + File.separator + "data" + File.separator : res_data +
> File.separator;
> public final static String datadir = "resources/data/";
> could some one help me in this regard .. by providing some short way to
> do this.
> I am currently searching for the string pattern say for example : "
> public static String res_data"
> and then appending "//" to $0(the whole line)..is there some other way
> to do it.
> Thanks .
my code that does the work is below :
/public static String res_data/ {print"//" $0;next}
/public final static String res_data/ {sub("//","",$0);print $0;next}
/public final static String datadir = \(res_data==null\)/ {print "//"
$0; next}
/public final static String datadir = \"resources\/data\/\"/
{sub("//","",$0);print $0;next}
{print $0}
please can some one tell me how to make a smaller code for this ?
Thanks ..
| |
| Ed Morton 2006-07-10, 6:56 pm |
| hector03@gmail.com wrote:
> hector03@gmail.com wrote:
>
>
> my code that does the work is below :
>
> /public static String res_data/ {print"//" $0;next}
> /public final static String res_data/ {sub("//","",$0);print $0;next}
> /public final static String datadir = \(res_data==null\)/ {print "//"
> $0; next}
> /public final static String datadir = \"resources\/data\/\"/
> {sub("//","",$0);print $0;next}
> {print $0}
>
> please can some one tell me how to make a smaller code for this ?
> Thanks ..
>
In your eample, the commenting always starts at the beginning of the
line, never in the middle, so if we assume that's always the case, then
you just need to do this:
a) replace all comment delimeters (i.e. //) with some other chars (e.g. @@)
b) prepend comment delimeters to every line that doesn't start with your
replacement chars (@@)
c) delete your replacement chars.
Like this (untested):
awk '{sub("//","@@")}!/@@/{printf("//")}{sub("@@","")}1' file
If "@@" appears in your input, pick some other chars (or there are ways
to avoid possible clashes if you really can't come up with some
impossible string).
As far as changing it back afterwards, just don't overwrite the original
file.
Regards,
Ed.
|
|
|
|
|