Quick and easy way to comment out multi lined print statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick and easy way to comment out multi lined print statements
# 1  
Old 04-09-2016
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?

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

# 2  
Old 04-09-2016
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.
# 3  
Old 04-10-2016
Quote:
Originally Posted by Don Cragun
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"

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

# 4  
Old 04-10-2016
How about "compiler directives" for conditional compiling? Add some #ifdef directives to create debug or nodebug versions in next to no time.
# 5  
Old 04-10-2016
Certainly not a generic solution, but it seems to work for your coding style:
Code:
#!/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:
Code:
            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:
Code:
            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");
	    }

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-10-2016
Quote:
Originally Posted by RudiC
How about "compiler directives" for conditional compiling? Add some #ifdef directives to create debug or nodebug versions in next to no time.
Can you explain how the #ifdef directive works? I have never used that before.

Quote:
Originally Posted by Don Cragun
Certainly not a generic solution, but it seems to work for your coding style:
Code:
#!/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:
Code:
            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:
Code:
            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");
        }


Thank you Smilie. 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.
# 7  
Old 04-10-2016
Look at e.g. https://gcc.gnu.org/onlinedocs/cpp/C...l#Conditionals or C# Compiler Directives - Stack Overflow and links therein. Although dealing with C#, it's about exactly your question.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

One Line Command how to use pipe statements to execute and comment on multiple possible outcomes

Hello Forum, I'm looking to expand the following command: INACTIVE_KERNELS=$(python -mplatform | grep -qi red && rpm -qa | grep '^kernel-' |grep -vE `uname -r` | paste -sd \; || echo "Not Red Hat Server") Currently this command will check if my server is RedHat server using the grep -qi... (6 Replies)
Discussion started by: greavette
6 Replies

2. Shell Programming and Scripting

Generate print statements

I need to pickup values provided in braces from a file with many records I am able to get the count of number of dept's in each line .How do we write a logic to generate a print statement like below instead of using a while loop if the count is 0 it should generate awk -F'' '{print $2 }' ... (1 Reply)
Discussion started by: llazydev
1 Replies

3. Shell Programming and Scripting

Sql multi line comment /* shell interpretation issue

Greetings Experts, I do have some basic knowledge of Unix. The task I am trying to do through shell script is to generate the view script for all of the tables which is in YYYYMMDD format (I assume I am on Ksh). I have certain tables that ends in YYYYMMDD format (eg: tbl_20150630) For each... (1 Reply)
Discussion started by: chill3chee
1 Replies

4. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

5. Shell Programming and Scripting

Extract values from multi lined url source

Hello, I want extract multi values from multi url source to a csv text. Thank you very much for help. my curl code : curl "http://www.web.com/cities//city.html Source code: div class="clear"></div> <table class="listing-details"> <tr> ... (1 Reply)
Discussion started by: hoo
1 Replies

6. Shell Programming and Scripting

Gawk - to extract values from multi lined file -I

Hi, Request your help in getting help with the below text formatting using awk. I am still learning awk and your help here is appreciated. Thanks in advance. Desireoutput ---------------- Virtual Pool Destination Profile Profile Profile Profile 1. virtual-1 pool-1 212.254.110.174:https... (2 Replies)
Discussion started by: pratheeshp
2 Replies

7. Shell Programming and Scripting

Gawk - to extract values from multi lined file

Hi, I am new to awk and trying to extract some specific fields from the a large file. Can you please help me to write gawk code displaying the out put in the below format: Desired Output: name fallback_ip member member www-trymps.extlb.plstry.com-pool-1 180.254.112.50 ... (4 Replies)
Discussion started by: pratheeshp
4 Replies

8. Shell Programming and Scripting

Help with for/if/else print statements

{for(i in b){if(b-1&&a!=b){print i";\t\t"b}else{print "NEW:"i";\t\t"b} } } what I need is to just print else{print "NEW:"i";\t\t"b and not print {print i";\t\t"b} how do i get awk to not print the first bit? is there an ignore statement although i still need awk to use this statement... (2 Replies)
Discussion started by: slashbash
2 Replies

9. Shell Programming and Scripting

Notepad help needed easy quick question

TAKE A LOOK AT THE ATTACHED PICTURE. my goal is just to SELECT n DELETE Those lines : Dialogue: Marked=0,0:02:39.49,0:02:40.49,Default,NTP,0000,00 00,0000,!Effect, (without the text) take a look at that picture... the marked line... i just need ALL of these lines removed.. i managed... (3 Replies)
Discussion started by: mr_spidey
3 Replies
Login or Register to Ask a Question