Script to replace/delete lines in C program

I am under the gun on a project and am not very good at scripting. I have to make a modification to thousands of C programs to basically replace the #pragma statement. I don't want to have to do it manually. Here is an example of what I need done. Any help would be greatly appreciated.

I have a number of C programs that have the following 2 scenarios

Scenario 1
First line of C program is
#pragma ident "@(#)program1.sc 1.1 03/11/09 XXXXX"
(always the 1st line of the program)
Scenario 2
First 3 lines of C program are

#ifndef WIN32
#pragma ident "@(#)program2.sc 1.3 06/11/09 EDS_dsct"
#endif
(always the 1st 3 lines of the program)

Final result
Depending on whether I have scenario 1 or scenario 2. I need to replace both scenarios with

#pragma ident "%W% %G% %Q%"

For scenario 1, I just need to replace one line for one line.
For scenario 2, I need to replace 3 lines with one line. I only need to replace the ifndef/endif in the 1st 3 lines. If there are other ifndef/endif statements in the program, they still need to remain.

Once again. Thanks for you help and it is greatly appreciated.
thanks.

Assuming I got what you want:

awk ' FNR==1 
        {ok=2; printf("#pragma ident %c%%W%% %%G%% %%Q%%%c\n", 34,34);}
       FNR==2 && /pragma ident/ 
        { ok=4}
       FNR>=ok 
        {print}
       {next}'   file.c > tmp.tmp
# if tmp.tmp is okay then uncomment the next line
#     mv tmp.tmp file.c

try this

Tried it. Didn't work. It did not replace the #pragma line. Probably something I am doing.

In your first scenario, is XXXXX a literal sequence of five X's or is that a placeholder for varying text? Similarly, for the dates and version numbers, are they invariant?

Regards,
Alister

The XXXXX could be anything. Basically, what I am trying to get at is the #pragma statement in scenario 1 and 2 needs to be replaced with the #pragma statement in the final result. The #pragma statement in scenario 1 and 2 are just examples. The date, program name, XXXXX don't really mean anything to me. I just want to replace that #pragma statement with the one from the final result. The #pragma in the final result is EXACTLY what it needs to be. Also for scenario 2, I need to replace the 3 lines with the 1 #pragma line from the Final Results. Thanks

The following script will process whatever filenames are passed on the command line. You can feed it with find ... -exec ... or xargs . Alternatively, you could swap out the for-loop for a while-read-loop and just pipe your filenames.

CAUTION: Run this at your own risk. I suggest you test it on copies of the real files, or be prepared to restore the originals if it doesn't work. I tested it lightly, on 3 small files (scenario 1, scenario 2, and a file that didn't match either).

#!/bin/sh

for f in "$@"; do
    sc=$(
    sed -n '
        # Scenario 1 prints "1"
        /#pragma ident/ {
            =
            q
        }

        # Scenario 2 prints "3"
        /#ifndef WIN32/! q
        n
        /#pragma ident/! q
        n
        /#endif/ =
        q
    ' "$f"
    )

    case $sc in
        1) addr=1 ;;
        3) addr=1,3 ;;
        *) continue ;;
    esac

    ed -s "$f" <<EOED
$addr d
i
#pragma ident "%W% %G% %Q%"
.
w
q
EOED

done

Regards,
Alister