Sponsored Content
Top Forums Shell Programming and Scripting Quick and easy way to comment out multi lined print statements Post 302970656 by Don Cragun on Sunday 10th of April 2016 03:06:18 AM
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:
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
All times are GMT -4. The time now is 11:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy