0

I am trying to use tr command or any similar command to perform a specific text manipulation to this block:

if (/*condition*/)
{
    statement1;
}
int a=3;
if (a)
{
    statement1;
    statement2;
}
else
{
    statement1;
    statement2;
    statement3;
    statement4;
    ///may be more lines
}

I want to remove single line commands and get this:

if (/*condition*/)
    statement1;
int a=3;
if (a)
{
    statement1;
    statement2;
}
else
{
    statement1;
    statement2;
    statement3;
    statement4;
    ///may be more lines
}

I've tried tr -s '{}' ' ' <file.txt but it squeezes all of the braces, not with the specific single line format.

Thanks

Mostafa Bahri
  • 2,303
  • 2
  • 19
  • 26

1 Answers1

0

You can set the record separator to either { or } and check how many fields you have. If there are more than one, print brackets around:

$ awk -v RS=[{}] 'NF>1{$0="{"$0"}"}1' file
if(true)


statement;


if(false)

{
statement1;
statement2;
}
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • It keeps empty lines in between. I am trying to figure out how to remove them. – fedorqui Jun 26 '15 at 10:28
  • @Potter6000 yep, however I am using a different Record Separator (RS) instead of the default new line. So this would work if we piped to the awk you suggest or any other tool removing empty lines. But I was hoping to find a one-command to do all together. – fedorqui Jun 26 '15 at 13:26
  • @Potter6000 update your question showing some representative data. I of course based my answer in the input you gave. If you were thinking in another type of input, edit your question. But try to stick to one thing, otherwise I don't think I will follow up. – fedorqui Jun 26 '15 at 13:28
  • Uhms, now I see my answer is quite fragile. I will probably delete it and see if I can improve it – fedorqui Jun 26 '15 at 13:37