1

I am trying to modify a if loop in a large code base.My need is as follows.

The code may contain as follows.This is just a random combination example of an if condition.I need to modify if else if conditions alone.

if((A==B)&&(C==D)&&((E==F)||(G==H))||(I)&&(J!=K))

should be modified as

 if((string.Compare(A,B)==0)&&(string.Compare(C,D)==0)&&((string.Compare(E,F)==0)||(string.Compare(G,H)==0))||(I)&&(string.Compare(J,K)!=0))

I tried with Java but utterly failed.I believe this is possible with sed or awk.Any help?

4
  • ANTLR is kind of perfect for that purpose :) Commented May 10, 2011 at 7:09
  • can't use ANTLR.Need shell or java or c# Commented May 10, 2011 at 7:21
  • -1 The question is really unclear. You should put some effort if you want to be understood. Don't just blame others. Commented May 10, 2011 at 8:10
  • I am sorry if my question was not clear.I will upvote your answer so that it does not affect your reputation. Commented May 10, 2011 at 9:19

3 Answers 3

2

You could do it basically with any language that supports regular expressions replacement.

Here's a 3 lines working C# example:

string text = "if((A==B)&&(C==D)&&((E==F)||(G==H))||(I)&&(J!=K))";
string pattern = @"\((?:(\w)((?:=|!)=)(\w))\)";
var replaced = Regex.Replace(text, pattern, m => string.Format("(string.Compare({0},{1}){2}0)", m.Groups[1].Value, m.Groups[3].Value, m.Groups[2].Value));

Console.WriteLine(replaced);

And the result:

if((string.Compare(A,B)==0)&&(string.Compare(C,D)==0)&&((string.Compare(E,F)==0)||(string.Compare(G,H)==0))||(I)&&(string.Compare(J,K)!=0))

Sign up to request clarification or add additional context in comments.

2 Comments

I guess this is what I want.I will try to get the equivalent of it in Java as I am not well versed with c#
@Harish Try doing it with Java. See how Java replaces regex matches, and then translate the code. The regular expression should be the same.
2
sed -r 's/([[:alpha:]])([=!]=)([[:alpha:]])/string.Compare(\1,\3) \2 0/g'

The spaces around \2 aren't strictly necessary.

2 Comments

Very interesting :) What does the 0/g at the end means?
@Oscar, "0" is just a zero, for the string.Compare boolean value. "/" marks the end of the replacement text for sed's s command. "g" means perform the search and replace globally in the text.
0

Basically you don't need to use awk or sed to construct the if statement. It is enough to write:

if [ A == B -a C == D -a ( E == F -o G == H ) -o I -a J != K ]; then
    your code here
fi

The == operator compares strings. If you wanted to compare numbers use -eq operator. See this chapter of bash manual (scroll down to test command), and the description of usage of primary conditional expressions

2 Comments

I'm almost sure that he's not talking about it. What he wants is to convert some code file syntax to another.
actually you got my question wrong.Its about manipulating the text inside if loop not about if loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.