Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline comment in awk

Tags:

awk

I would like to know how to use multi line comment in awk. As of now I have been using # to comment a single line. Could someone guide me regarding this. Thank you.

like image 533
NandaKumar Avatar asked Jul 20 '12 07:07

NandaKumar


People also ask

How do you use multiple lines in awk?

One technique is to use an unusual character or string to separate records. For example, you could use the formfeed character (written ' \f ' in awk , as in C) to separate them, making each record a page of the file. To do this, just set the variable RS to "\f" (a string containing the formfeed character).

How do you comment multiple lines?

To comment out multiple code lines right-click and select Source > Add Block Comment. ( CTRL+SHIFT+/ )

How do you comment inside awk?

In the awk language, a comment starts with the number sign character (' # ') and continues to the end of the line. The ' # ' does not have to be the first character on the line. The awk language ignores the rest of a line following a number sign.

What is awk F?

For example: awk -F, ' program ' input-files. sets FS to the ' , ' character. Notice that the option uses an uppercase ' F ' instead of a lowercase ' f '. The latter option ( -f ) specifies a file containing an awk program.


1 Answers

There is no multiline comment in AWK, but you can fake it if you need to. Here is one technique that works at least in GNU AWK (gawk):

#!/usr/bin/awk -f
0 {
    You can use
     0 to cause
     a block to
     not execute
     or be parsed
}

{
    print $2, $1, $3
    if (0) {
        You can use if (0)
        in a similar manner
        inside a block
    }
    sum += $4
}

0 && /pattern/ {    # prepend "0 &&" to other conditions to turn off a block
    print
}

It's nice to be able to have multiline comments for commenting out sections of code during debugging. I wouldn't necessarily use this technique for documentation since it may not be guaranteed that the non-code text would not be parsed for syntax errors.

It seems to also work in mawk.

like image 142
Dennis Williamson Avatar answered Oct 02 '22 22:10

Dennis Williamson