passing shell vars to gawk 
Author Message
 passing shell vars to gawk

Hello,

I've written a script, using bash and gawk, that will convert csv files to html tables.  I'm
have trouble.  Here is the script:
****
#! /bin/bash
#awk script to create HTML tables from CSV files
#usage table <csv-file> <caption>

CSV=$1
CAPTION=$2

awk -F,  '
BEGIN {
        print "<table border cellspacing=0 cellpadding=5>"
        print "<caption>" $CAPTION"</caption>"
        }
{
x=1
print"<tr>"
        while ( x <= NF ) {
                if ( NR ==1) print "\t "<th>" $x "<th>"
                else print "\t <td>" $x "<td>"
                ++x
                }
        print "</tr>"

Quote:
}

END {
        print "</table>"
Quote:
}' $CSV

*****

My problem is that I cannot transfer the $2 bash argument to the awk
script.  I've tried:

print "<caption>" ARGV[2] "</caption>"

awk -F -v CAPTION="$CAPTION"

awk -F -v CAPTION=$CAPTION

BEGIN{
        caption="'"$CAPTION"'"

Nothing seems to work.  I know it's something simple that I don't yet
know but I can't figure out what it is.  Will someone help me out?

Thanks,
--
Neil Watson
Network Administrator
www.watson-wilson.ca



Thu, 04 Dec 2003 05:56:02 GMT  
 passing shell vars to gawk

Quote:

>        print "<caption>" $CAPTION"</caption>"

$CAPTION is the _field_ with number CAPTION. Use CAPTION without the
dollar sign and this line:

Quote:
> awk -F -v CAPTION="$CAPTION"

        ^
with your original comma there.

Quote:
> Nothing seems to work.  I know it's something simple that I don't yet
> know but I can't figure out what it is.  Will someone help me out?

Just a typo/seeo...

Regards...
                Michael



Thu, 04 Dec 2003 07:29:56 GMT  
 passing shell vars to gawk
Excellent. It works perfectly now.  Thank you.

Quote:


>>        print "<caption>" $CAPTION"</caption>"
> $CAPTION is the _field_ with number CAPTION. Use CAPTION without the
> dollar sign and this line:
>> awk -F -v CAPTION="$CAPTION"
>         ^
> with your original comma there.

--
Neil Watson
Network Administrator
www.watson-wilson.ca


Thu, 04 Dec 2003 12:41:20 GMT  
 passing shell vars to gawk

% I've written a script, using bash and gawk, that will convert csv files to html tables

If all you use bash for is argument parsing, you could eliminate it:

Replace
% #! /bin/bash
with
  #!/usr/bin/awk -f

and replace

% CSV=$1
% CAPTION=$2
%
% awk -F,  '

with
  BEGIN {
     FS = ","

     # you might want to check the arguments here...

     caption = ARGV[2]
     ARGC = 2
  }

then replace each instance of $CAPTION with caption and finally, replace

% }' $CSV

with
  }

There's more than one way to skin a cat, and my favourite ways avoid using
the shell....
--

Patrick TJ McPhee
Knightsbridge SW7



Sun, 07 Dec 2003 22:15:53 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. passing vars to shell script

2. #!/bin/awk -f style and shell vars

3. Shell vars into Awk executable script not so easy

4. Accessing UNIX shell vars from lisp

5. How to use shell vars in a script?

6. Quoting with GAWK in OS/2 cmd Shell

7. passing vars to awk script

8. Passing and getting vars. from C to awk

9. HELP: Passing Unix vars to AWK

10. passing run-time vars to RRUN

11. Accessing Vars in Group Passed by Address

 

 
Powered by phpBB® Forum Software