Quick, simple, effective, over-enthusiastic
It is fairly simple to comment out all the printf()
lines in a file with sed
, even multi-line ones, if you use:
sed -e '/printf(.*);/ { s%^%//%; n; }' -e '/printf(/,/);/ s%^%//%'
On the sample lines, this yields:
lower_bound_of_big_boy_counter++;
// printf("3387 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n",
// strings_line_tokens[lower_bound_of_big_boy_counter]);
// printf("3389 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
}
strcpy(array_id1, strings_line_tokens[lower_bound_of_big_boy_counter]);
integer_match_flag = 0;
float_match_flag = 0;
}
}
if(keywords_match_flag1 != 1)
{
lower_bound_of_big_boy_counter++;
}
cmp_str9 = strcmp("return", strings_line_tokens[lower_bound_of_big_boy_counter ]);
// printf("4006 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
// printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n",
// strings_line_tokens[lower_bound_of_big_boy_counter]);
//int
if(cmp_str9 == 0)
{
// printf("3402 checking return stuff \n");
return_match_flag = 1;
lower_bound_of_big_boy_counter++;
get_function_type_for_proper_return(symbol_table_functions, type,id,
scope_char, symbol_table_functions_counter, function_type_for_proper_return);
// printf("3407 lower_bound_of_big_boy_counter %d \n", lower_bound_of_big_boy_counter);
// printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n",
// strings_line_tokens[lower_bound_of_big_boy_counter]);
// printf("3410 function_type_for_proper_return %s \n", function_type_for_proper_return);
However, if any of the printf()
statements contained ACCEPT
or Reject
, those lines would be commented out too. Given that you have hundreds of lines that should be commented out and a few (order of ten) that should not be commented, it is probably easiest to undo the incorrectly commented out lines.
Refined, complex, effective
I got an alternative working, and it handles printf
statements with ACCEPT
and Reject
in the first line correctly. It won't spot those words in the second or subsequent lines of a printf
statement.
${SED:-sed} \
-e '/printf(.*ACCEPT.*);/ b' \
-e '/printf(.*Reject.*);/ b' \
-e '/printf(.*);/ { s%^%//%; b
}' \
-e '/printf(.*ACCEPT/, /);/ b' \
-e '/printf(.*Reject/, /);/ b' \
-e '/printf(/, /);/ s%^%//%' \
frag.c
The odd newline is need to placate BSD (Mac OS X) sed
.
Input:
lower_bound_of_big_boy_counter++;
printf("3387 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
printf("3389 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
}
strcpy(array_id1, strings_line_tokens[lower_bound_of_big_boy_counter]);
integer_match_flag = 0;
float_match_flag = 0;
}
}
if(keywords_match_flag1 != 1)
{
lower_bound_of_big_boy_counter++;
}
cmp_str9 = strcmp("return", strings_line_tokens[lower_bound_of_big_boy_counter ]);
printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
wimbol();
printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
wimbol();
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
wimbol();
printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
wimbol();
printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
wimbol();
printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
wimbol();
printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
wimbol();
printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
//int
if(cmp_str9 == 0)
{
printf("3402 checking return stuff\n");
return_match_flag = 1;
lower_bound_of_big_boy_counter++;
get_function_type_for_proper_return(symbol_table_functions, type,id,
scope_char, symbol_table_functions_counter, function_type_for_proper_return);
printf("3407 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
printf("3410 function_type_for_proper_return %s\n", function_type_for_proper_return);
Output:
lower_bound_of_big_boy_counter++;
// printf("3387 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
// strings_line_tokens[lower_bound_of_big_boy_counter]);
// printf("3389 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
}
strcpy(array_id1, strings_line_tokens[lower_bound_of_big_boy_counter]);
integer_match_flag = 0;
float_match_flag = 0;
}
}
if(keywords_match_flag1 != 1)
{
lower_bound_of_big_boy_counter++;
}
cmp_str9 = strcmp("return", strings_line_tokens[lower_bound_of_big_boy_counter ]);
// printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
wimbol();
printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
wimbol();
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
wimbol();
printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
wimbol();
// printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
// strings_line_tokens[lower_bound_of_big_boy_counter]);
wimbol();
printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
wimbol();
printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
wimbol();
// printf("4006 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("ACCEPT: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
printf("Testing ACCEPT %d\n", lower_bound_of_big_boy_counter);
printf("Reject: lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
// printf("4007 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
// strings_line_tokens[lower_bound_of_big_boy_counter]);
printf("Reject: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
printf("ACCEPT: strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
strings_line_tokens[lower_bound_of_big_boy_counter]);
//int
if(cmp_str9 == 0)
{
// printf("3402 checking return stuff\n");
return_match_flag = 1;
lower_bound_of_big_boy_counter++;
get_function_type_for_proper_return(symbol_table_functions, type,id,
scope_char, symbol_table_functions_counter, function_type_for_proper_return);
// printf("3407 lower_bound_of_big_boy_counter %d\n", lower_bound_of_big_boy_counter);
// printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s\n",
// strings_line_tokens[lower_bound_of_big_boy_counter]);
// printf("3410 function_type_for_proper_return %s\n", function_type_for_proper_return);
I was previously having some issues; I am kicking myself for some of them (sequencing — it is crucial to deal with all the single-line printf()
statements before starting on the multi-line ones). There's a more subtle difference between b
(jump to end of script) and n
(read next line), which is also crucial.
I'm testing with BSD sed
on Mac OS X 10.11.4, JFTR. GNU sed
doesn't require the extra newline in the script.