Script to replace/delete lines in C program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to replace/delete lines in C program
# 1  
Old 08-06-2012
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.
# 2  
Old 08-06-2012
Assuming I got what you want:
Code:
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
# 3  
Old 08-06-2012
Tried it. Didn't work. It did not replace the #pragma line. Probably something I am doing.
# 4  
Old 08-06-2012
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
# 5  
Old 08-06-2012
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
# 6  
Old 08-07-2012
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).

Code:
#!/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

Last edited by alister; 08-07-2012 at 02:04 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to merge and delete lines

POLY_STATS { EqName 103_tri Id 123 act_polyCount 1 act_polyValue 0 } POLY_STATS { EqName 103_tri Id 123 pass_polyCount 2 pass_polyValue 0 } POLY_STATS { EqName 103 Id 123 mes_polyCount 2 mes_polyValue 0 (5 Replies)
Discussion started by: Jag02
5 Replies

2. Shell Programming and Scripting

How to delete the lines from file using script?

Hi Iam having file like below 10.238.52.65 pun-ras-bng-mhs-01 server 10.238.52.65 pun-ras-bng-mhs-01 10.10.10.10 10.238.52.65 pun-ras-bng-mhs-01 10.10.20.10 10.238.54.1 enk-ras-bng-cse-01 server 10.238.54.1 enk-ras-bng-cse-01 10.10.30.10 10.238.54.1 enk-ras-bng-cse-01 10.10.10.10 ... (5 Replies)
Discussion started by: surender reddy
5 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

4. Shell Programming and Scripting

script to delete lines from a txt file if pattern matches

File 6 dbnawldb010-b office Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/09/11 03:24:04 42 luigi-b IPNRemitDB Memphis_Corp_SQL_Diff Memphis-Corp-SQL-Inc-Application-Backup 03/10/11 00:41:36 6 ebs-sqldev1-b IPNTracking Memphis_Corp_SQL_Diff... (4 Replies)
Discussion started by: ajiwww
4 Replies

5. Shell Programming and Scripting

looking for a script that will delete lines in a text file

it will grep for a line and then delete these line. how do i begin to write this script if theres no available one? (3 Replies)
Discussion started by: garfish
3 Replies

6. Shell Programming and Scripting

HP Unix Script to Delete the lines in a file

Hi Experts, I have a file format as mentioned below. I would like to have unix script (HP Unix) which can: 1. Remove first 6 and last 3 lines. 2. Delete the lines where 3rd column having Alpha Numeric Number 3. Delete the lines where 4th column having 0.00 4. Calculate the sum of all the... (16 Replies)
Discussion started by: phani333
16 Replies

7. Shell Programming and Scripting

Delete lines from file using Unix Script

Hi Experts, I have a file in the below given format. First two lines are header and Trailer. Rest all are transaction Lines. I have to delete all other lines except first line (Header) and lines which contains 5000 in 1st column and 0 in 5th column. Can anyone please kindly provide me with... (6 Replies)
Discussion started by: phani333
6 Replies

8. Shell Programming and Scripting

Need Shell Script to delete lines in a file

Hi All, I have a file with 3 columns (Bank Name, Account Number and Amount). My requirement, I need to delete lines using Unix Shell script: 1. Which are having Alphanumeric characters in Account Number (eg. Line3). 2. Which are having 0.00 in amount. (eg. Line4) 3. And also I need to... (4 Replies)
Discussion started by: phani333
4 Replies

9. Shell Programming and Scripting

Delete lines at several places in a file with script

Hi, I am a newbie to shell scripting, and I have a file which quite large which I would like to delete lines at certain places. I want to search for a keyword which is recurring in the file. When matched I would like to delete the line. And when the file was so huge I thought I ought to learn... (3 Replies)
Discussion started by: mr_andrew
3 Replies

10. Shell Programming and Scripting

How to Delete all lines in a file from a script

I am trying to delete all the lines out a file from a unix script. Please help Platform is Sun (3 Replies)
Discussion started by: alnita
3 Replies
Login or Register to Ask a Question