Quick and easy way to comment out multi lined print statements

Is there a quick and easy way to comment out multi lined print statements? something like this?

 printf("3408 strings_line_tokens[lower_bound_of_big_boy_counter] %s \n", 
            strings_line_tokens[lower_bound_of_big_boy_counter]);

The description you have provided is a little sparse. If this is C or C++ code, the following two options might help:

  1. For the example shown, you could search for lines starting with arbitrary whitespace followed by printf( as the start line and continue to the next line ending with ); followed by arbitrary whitespace.
  2. If you're in vi at the start of the line containing printf you could use the % command to find the ending ) (assuming that the format string and any string constants being printed don't contain any mismatched parentheses).

If you're not already inside an editor making changes to your source code, you could use ed , ex , sed , or awk , to make similar changes (assuming you want to comment out ALL printf statements or only want to comment out printf statements with a specific format string).

But, if this is awk code (where the trailing semicolon is optional and in come cases the parentheses are also optional), different rules would be needed.

I have a lot of c code like this. I just put in a ridiculous amount of printf statements for debugging and I want to comment all of them out except the ones that contain "ACCEPT" and "REJECT"

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

How about "compiler directives" for conditional compiling? Add some #ifdef directives to create debug or nodebug versions in next to no time.

Certainly not a generic solution, but it seems to work for your coding style:

#!/bin/ksh
awk '
$1 ~ /^printf[(]/ {
	NoChange = $0
	Comment = "//" $0
	while($NF !~ /[)];$/) {
		getline
		NoChange = NoChange "\n" $0
		Comment = Comment "\n//" $0
	}
	print (NoChange ~ /ACCEPT|REJECT/) ? NoChange : Comment
	next
}
1' ${1:-file.c}

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk ( nawk on Solaris/SunOS systems won't work with this awk script either).

If you invoke this script with no operands and the file file.c contains:

            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);
	    	printf("This is a one line printf containing ACCEPT.\n");

		printf("This is a multi-line printf containing %s%s.\n",
			"some more text",
			" ACCEPT");
	    }
            } else {
	    	printf("This is a one line printf containing REJECT.\n");

		printf("This is a multi-line printf containing %s.\n",
			"REJECT");
	    }

it produces the output:

            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);
	    	printf("This is a one line printf containing ACCEPT.\n");

		printf("This is a multi-line printf containing %s%s.\n",
			"some more text",
			" ACCEPT");
	    }
            } else {
	    	printf("This is a one line printf containing REJECT.\n");

		printf("This is a multi-line printf containing %s.\n",
			"REJECT");
	    }
1 Like

Can you explain how the #ifdef directive works? I have never used that before.

Thank you :). I will test that out when I get to work tomorrow.

Glad I don't have to deal with that Solaris/SunOS system issue. I mainly stick to Redhat on my server and Ubuntu on my regular PC's.

Look at e.g. https://gcc.gnu.org/onlinedocs/cpp/Conditionals.html\#Conditionals or C# Compiler Directives - Stack Overflow and links therein. Although dealing with C#, it's about exactly your question.