If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > questions about awk script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-24-04, 15:15
julytli01 julytli01 is offline
Registered User
 
Join Date: Feb 2004
Posts: 7
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!
Reply With Quote
  #2 (permalink)  
Old 02-24-04, 15:22
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 02-24-04, 15:31
julytli01 julytli01 is offline
Registered User
 
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.

Quote:
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.
Reply With Quote
  #4 (permalink)  
Old 02-24-04, 16:06
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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 :
Quote:
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 :
Quote:
#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.
Reply With Quote
  #5 (permalink)  
Old 02-24-04, 16:47
julytli01 julytli01 is offline
Registered User
 
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.

Quote:
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 :
Reply With Quote
  #6 (permalink)  
Old 02-24-04, 17:41
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
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 17:56.
Reply With Quote
  #7 (permalink)  
Old 02-25-04, 09:07
julytli01 julytli01 is offline
Registered User
 
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!
Quote:
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.
Reply With Quote
  #8 (permalink)  
Old 02-25-04, 09:25
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #9 (permalink)  
Old 02-25-04, 09:41
julytli01 julytli01 is offline
Registered User
 
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.

Quote:
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)"
Reply With Quote
  #10 (permalink)  
Old 02-25-04, 09:54
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #11 (permalink)  
Old 02-25-04, 10:09
julytli01 julytli01 is offline
Registered User
 
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.


Quote:
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 ..."
Reply With Quote
  #12 (permalink)  
Old 02-25-04, 10:21
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On