Monday, June 30, 2008

Range Pattern in AWK

I came across this post regarding removing #ifdef code from C program. Although the thread is closed for no obvious reason, it is pretty interesting from the shell script view point. The original post is like this:
Removing lines of code defined under flag

I want to write a shell script that will remove lines of C code that is defined under a certain flag, for eg, "#ifdef PRODUCT" in all the C files in a directory. Please help me somebody..I'm clueless!!

In C compiler, you should be able to run through the pre-processor (-E flag) to ignore the "#ifdef PROD" code by defining all the other conditional compilation directives with the "-D" flag. However, the preprocessing also include the content of the header files. This is definitely not the solution we are looking for.

In AWK, you can specify a range pattern and apply an action statement. Suppose we have this simple.c C program and our job is to remove the #ifdef PROD block of code.

#include <stdio.h>

int main(void) {

        #ifdef PROD
        printf("Prod is here\n");
        #endif

        #ifdef DEV
        printf("Dev is here\n");
        #endif

        #ifdef PROD
        printf("Prod is there\n");
        #endif
}

Below AWK code skips to the next input record when it is within the range pattern. The start range pattern is any whitespace followed by #ifdef PROD and the end range pattern is any whitespace followed by #endif. Otherwise, it just simply print it out.

$ awk '
/^[ \t]*#ifdef PROD/,/^[ \t]*#endif/ {
next
}
{
print
}' sample.c
#include <stdio.h>

int main(void) {


        #ifdef DEV
        printf("Dev is here\n");
        #endif

}

Range pattern in AWK is particularly useful and very often people deliberately plant their own start and end patterns in their data stream for future processing. May be you can start to think along this line.

Labels:

0 Comments:

Post a Comment

<< Home