awk routine help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk routine help
# 15  
Old 04-22-2009
Hope This too will help..

awk '{ printf ( "%s \b%s", $1 , "," ) }' file1
# 16  
Old 04-22-2009
Quote:
Originally Posted by giannicello
To run it from your script have you tried wrapping awk ' .. ' around it?

Code:
awk '
{
   aN[FNR]=$1; aT[FNR]=($2 !~ "integer")?q:""
}
END{
  printf("insert into table (")
  for(i=1;i<=FNR; i++)
     printf("%s%c", aN[i], (i==FNR)?")":OFS)

  printf(" values (")
  for(i=1;i<=FNR; i++)
     printf("%cnew.%s%c%s", aT[i], aN[i], aT[i],(i==FNR)?")" ORS:OFS)
}
'

That doesn't seem to work. I think it has to do with needing the parameters you left out that are used when called: ( q="'" OFS=, ).
I tried to put those at the top of the script with a BEGIN statement but I get a syntax error still.
# 17  
Old 04-22-2009
Heya!
If you don't want a separate awk script, you can put everything inline in your shell wrapper script like so:
Code:
#!/bin/ksh
...................
...................
nawk '
{
   # an array "aN" of fields NAMES indexed by FNR   
   aN[FNR]=$1

   # an array "aT" of fields TYPES indexed by FNR 
   # if the SECOND field contains a string "integer", the value of an array
    #cell is a single quote, and an empty string "" otherwise
   aT[FNR]=($2 !~ "integer")?q:""
}
END{

  # print the "prefix" of the insert statement
  printf("insert into table (")
 
  # iterate through the of field NAMES (aN) indexed by FNR
  # printing the field names separated by OFS (,).
  # When at the LAST entry in the array (i==FNR), output the training ")"
  for(i=1;i<=FNR; i++)
     printf("%s%c", aN[i], (i==FNR)?")":OFS)

  printf(" values (")

  # iterate through the of field TYPES (aT) indexed by FNR
  # printing the field names separated by OFS (,).
  # When at the LAST entry in the array (i==FNR), output the training ")"
  # followed by ORS (newLine).
  # "aT[i]" will correspond to either a single quote (for integers), or to
  # nothing (empty string) for the other types.
  for(i=1;i<=FNR; i++)
     printf("%cnew.%s%c%s", aT[i], aN[i], aT[i],(i==FNR)?")" ORS:OFS)
}' q="'" OFS=, myFile

'man nawk' yields:
Code:
     FNR   The ordinal  number  of  the  current  record  in  the
           current file. Inside a BEGIN action the value is zero.
           Inside an END action the value is the  number  of  the
           last record processed in the last file processed.

     OFS   The print statement output field  separator;  a  space
           character by default.

     ORS   The print output record separator; a newline character
           by default.


Last edited by vgersh99; 04-22-2009 at 09:45 AM..
# 18  
Old 04-22-2009
Hey Vger,
Thanks so much for that well documented script. I'm beginning to get it now.
I have a question. There are times when the printf statement will need to output 'old.column' instead of 'new.column'.

So instead of writing the whole code twice, I think I can pass a parameter from the shell script and have awk evaluate that in an if statement to decide which printf to do.

I tried this but it doesn't work. what I did was,
-- at the end of the script where you have:
}' q="'" OFS=, myFile
I changed that to }' q="'" OFS=,type="'update'" myFile

Then I made an if statement where you have this:
printf("%cnew.%s%c%s", aT[i], aN[i], aT[i],(i==FNR)?")" ORS:OFS)

I changed it to this:

if( type == "update" )
{
printf("%cnew.%s%c%s", aT[i], aN[i], aT[i],(i==FNR)?")" ORS:OFS)
}

I figured if I can get that to work, I can put the else or elseif statements in there later.

But I got something wrong here. Can you help ?

Thank you !!

Floyd
# 19  
Old 04-22-2009
ok.
Code:
q="'" OFS=, type='update' myFile

....and
Code:
printf("%c%s.%s%c%s", aT[i], (type=="update")?"new":"old", aN[i], aT[i],(i==FNR)?")" ORS:OFS)

# 20  
Old 04-22-2009
Hey Vger,
I think I get the switch statement you're doing inline there, but I still am not seeing, or able to get the type into the nawk routine this way.

I will be calling the nawk routine with an argument that will tell it whether to use old or new.
I don't see that working here.

You have: q="'" OFS=, type='update' myFile, I don't understand what that is spposed to do. Shouldn't it be setting a variable for nawk based on an argument I give it ?

Thank you,
Floyd
# 21  
Old 04-22-2009
Quote:
Originally Posted by fwellers
Hey Vger,
I think I get the switch statement you're doing inline there, but I still am not seeing, or able to get the type into the nawk routine this way.

I will be calling the nawk routine with an argument that will tell it whether to use old or new.
I don't see that working here.

You have: q="'" OFS=, type='update' myFile, I don't understand what that is spposed to do. Shouldn't it be setting a variable for nawk based on an argument I give it ?

Thank you,
Floyd
That's right, it assigns the awk variable 'type' a value of 'update'. The variable 'type' is used in the 'printf' statement later on in the code.

If you need to have 'old.fname' if you do 'type=update', then change the printf to:
Code:
printf("%c%s.%s%c%s", aT[i], (type=="update")?"old":"new", aN[i], aT[i],(i==FNR)?")" ORS:OFS)

I was not sure what logic you wanted to implement.

Post the exact calling sequence and the awk code and the output you're getting AND the desired output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to Parse An Inherited Command/Routine

I am am one of these people that it isn't good enough just to say, "Here, try this...". it is important for me to understand how and why something works (or doesn't work.) All that being said, I am trying to parse out a command that we use that was handed down to me by someone I can no longer... (3 Replies)
Discussion started by: he204035
3 Replies

2. Shell Programming and Scripting

PERL: Calling a sub routine from another module - help!!!

Hi, I am an occasional PERL user. I am trying to call a sub routine (passing parameters) in perl module from a driver .pl file. I have been "tinkering" for a while now and have confused myself. Could someone please look at the code below and spot where I am going wrong. testPerl.pl ... (0 Replies)
Discussion started by: chris01010
0 Replies

3. Shell Programming and Scripting

Paramerter pass for function(sub routine) need help

Hi, Please help me here while passing the paramert to fuction i am facing problem. i tryied passing 7 PARAMeter in side single quote,double quate even tried tild sign not working. how can assign it properly . usage () { typeset -i NumPARAMs=$1 typeset -i PARAM1=$2 typeset PARAM2=$3... (3 Replies)
Discussion started by: nitindreamz
3 Replies

4. Shell Programming and Scripting

Perl - Call a sub routine in a command

Hi all I have written a simple perl script that has different options i.e. myscript -l -p etc i have it so when it runs without any switches it runs a subroutine called nvrm_norm i want to be able to do a -p option and run pall -w -f and then called the subruotine pall is... (1 Reply)
Discussion started by: ab52
1 Replies

5. Shell Programming and Scripting

File exists routine

Hello experts, I need some help here.. I've written the following routine to check for existence of files. The routine does the following. It will look for a compressed ( .Z ) file and if it exists, then it will uncompress it, if it is already uncompressed, then it will just diplay a message... (9 Replies)
Discussion started by: kamathg
9 Replies

6. Shell Programming and Scripting

how to cp files to dir,using routine?

hi all, I wanted to know how we can copy files to dirs, through a routine and when the file and the dir are specified as parameters for that routine and explicitly called? Eg: suppose i want to copy file1 to /tmp then myproc() { . . } myproc /path/file1 /tmp/ These parameters when... (4 Replies)
Discussion started by: wrapster
4 Replies

7. UNIX for Advanced & Expert Users

how to cp files to dir,using routine?

hi all, I wanted to know how we can copy files to dirs, through a routine and when the file and the dir are specified as parameters for that routine and explicitly called? Eg: suppose i want to copy file1 to /tmp then myproc() { . . } myproc /path/file1 /tmp/ These parameters when... (1 Reply)
Discussion started by: wrapster
1 Replies

8. Shell Programming and Scripting

sub routine call

in windows machine... C:\2\test>perl -version This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) ------------------------------------------ what is the difference b\w subroutine calls: sub_routine_name("-----"); and ... (2 Replies)
Discussion started by: sekar sundaram
2 Replies

9. UNIX for Dummies Questions & Answers

Routine Task being a Solaris Administrator

Hi, What are routine task being a solaris administrator ? Thanks, Far (1 Reply)
Discussion started by: Far
1 Replies

10. Programming

Entry Points Routine

How do we pronounciate bdevsw and cdevsw Kernel resources ? I presume it as block or charcter device software table. Am I Correct in my assumption ? Thanks in advance. (9 Replies)
Discussion started by: S.P.Prasad
9 Replies
Login or Register to Ask a Question