Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2004
    Posts
    7

    Unanswered: questions about awk script

    Hi,
    I have a bunch of codes (like 80), every code has several subroutines (There are altogether almost 500 subroutines). The task I was posed is to add C preprocessing to each subroutine (add #ifdef before each subroutine). I think this can be done using awk script. Could anybody help me on this? Please send email to julytli01@yahoo.com, thanks a lot!

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Do you want to add and '#ifdef' statement only ?
    You do you identify a subroutine ?

    Give us an example of input code and requested result.
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    7
    Look at the following example:

    subroutine 1

    ...
    return
    end

    what I want to do is to produce

    #ifdef sub1
    subroutine 1

    ...
    return
    end
    #endif

    But there are about 500 subroutines to do, so I want to use a smarter way to do that. I think the key issue in awk script is how to identify the string"subroutine", but right now I don't know how to do that.
    Many thanks.

    Originally posted by aigles
    Do you want to add and '#ifdef' statement only ?
    You do you identify a subroutine ?

    Give us an example of input code and requested result.

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try and adapt the following script (save your files before) :

    Usage: the_script input_file(s)

    Code:
    for code_file in ${*:?"Specify input file(s)"}
    do
       echo "Processing file $code_file ..."
       temp_file=$code.tmp
       awk '
            $1=="subroutine" {
               print "#ifdef sub" $2;
            }
            {
               print $0;
               prv_statement = new_statement;
               new_statement = $1;
            }
            $1=="end" && prv_statement == "return" {
               print "#endif";
            }
            ' $code_file > $temp_file
        if [ $? -eq 0 ]
           then mv $temp_file $code_file
           else echo "Failed !!!"
        fi
    done
    With the following input file :
    subroutine aaa
    code 1/2
    code 2/2
    return
    end

    subroutine bbb
    code 1/3
    code 2/3
    code 3/3
    return
    end
    the result is :
    #ifdef subaaa
    subroutine aaa
    code 1/2
    code 2/2
    return
    end
    #endif

    #ifdef subbbb
    subroutine bbb
    code 1/3
    code 2/3
    code 3/3
    return
    end
    #endif
    Jean-Pierre.

  5. #5
    Join Date
    Feb 2004
    Posts
    7
    Do I need to add the line" for code_file ..." in the script? I named the following script as awk.txt and the input file as input, then type:
    ./awk.txt input
    It does not work, I replaced Specify input file(s) with "input", should I replace code_file with something? could you be more specific?
    Many thanks.

    Originally posted by aigles
    Try and adapt the following script (save your files before) :

    Usage: the_script input_file(s)

    Code:
    for code_file in ${*:?"Specify input file(s)"}
    do
       echo "Processing file $code_file ..."
       temp_file=$code.tmp
       awk '
            $1=="subroutine" {
               print "#ifdef sub" $2;
            }
            {
               print $0;
               prv_statement = new_statement;
               new_statement = $1;
            }
            $1=="end" && prv_statement == "return" {
               print "#endif";
            }
            ' $code_file > $temp_file
        if [ $? -eq 0 ]
           then mv $temp_file $code_file
           else echo "Failed !!!"
        fi
    done
    With the following input file :


    the result is :

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by julytli01
    Do I need to add the line" for code_file ..." in the script? I named the following script as awk.txt and the input file as input, then type:
    ./awk.txt input
    It does not work, I replaced Specify input file(s) with "input", should I replace code_file with something? could you be more specific?
    Many thanks.
    You'll need the code block as posted in its entirety. 'code_file' is the name of a variable to which the values of your input parameters will be assigned (i.e. your input files). What error are you getting? Your script is probably not executable. Try 'chmod 755 awk.txt' and then run it again.
    Last edited by Damian Ibbotson; 02-24-04 at 18:56.

  7. #7
    Join Date
    Feb 2004
    Posts
    7
    my script is executable. When I run it, it has the error information like:
    "line 2: syntax error near unexpected token 'do". I checked with line 2 and did not find ', there is only do in line 2. I copied the whole script as you pasted and the only change is to change "specify input file(s)" to input, where my input is the simple two subroutines as you used.
    Thanks a lot!
    Originally posted by Damian Ibbotson
    You'll need the code block as posted in its entirety. 'code_file' is the name of a variable to which the values of your input parameters will be assigned (i.e. your input files). What error are you getting? Your script is probably not executable. Try 'chmod 755 awk.txt' and then run it again.

  8. #8
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The error must be in the for statement.
    Show us your modification.


    Restor as posted, and execute the script with the input filename(s) as argument.

    ${*:?"Specify input file(s)"}
    Substitute argument passed. If no argument on command line then print the error message "Specify input file(s)"
    Jean-Pierre.

  9. #9
    Join Date
    Feb 2004
    Posts
    7
    The whole content of the script I used is:

    for code_file in ${*:?"Specify input file(s)"}
    do
    echo "Processing file $code_file ..."
    temp_file=$code.tmp
    awk '
    $1=="subroutine" {
    print "#ifdef sub" $2;
    }
    {
    print $0;
    prv_statement = new_statement;
    new_statement = $1;
    }
    $1=="end" && prv_statement == "return" {
    print "#endif";
    }
    ' $code_file > $temp_file
    if [ $? -eq 0 ]
    then mv $temp_file $code_file
    else echo "Failed !!!"
    fi
    done

    exactly the same as yours. I saved the above script as awk.txt and change it to executable, then type ./awk.txt. It still has the error information as before: line 2 syntax error near unexpected token 'do.
    Is there any format requirement on script? like in makefile, the command must start with a Tab.
    I don't understand why.

    Originally posted by aigles
    The error must be in the for statement.
    Show us your modification.


    Restor as posted, and execute the script with the input filename(s) as argument.

    ${*:?"Specify input file(s)"}
    Substitute argument passed. If no argument on command line then print the error message "Specify input file(s)"

  10. #10
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    There is no format requirement on script.
    What is you loging shell ?

    Modify the script and test :

    Code:
    #!/usr/bin/sh
    for code_file in ${*:?"Specify input file(s)"}
    do
       echo "Processing file $code_file ..."
    Jean-Pierre.

  11. #11
    Join Date
    Feb 2004
    Posts
    7
    Then it will have the following error information:
    bash ./awk.txt bad interpreter: No such file or directory.
    What's the shell requirement on running an awk script? Sorry to bother you so much, I am a beginner on this. thanks.


    Originally posted by aigles
    There is no format requirement on script.
    What is you loging shell ?

    Modify the script and test :

    Code:
    #!/usr/bin/sh
    for code_file in ${*:?"Specify input file(s)"}
    do
       echo "Processing file $code_file ..."

  12. #12
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    The script is written for sh, bash or ksh.
    Awk can run whith every shell.

    Enter the command : which sh

    The result will be the full path of the shell interpreter for 'sh'
    This is this path that must be specified in the first line of the script.

    To determine what shell is yours, enter the command : set | grep -i shell
    Jean-Pierre.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •