awk doesn't take "here" document? 
Author Message
 awk doesn't take "here" document?

Hi there,

I wrote something like this:

    #!/bin/bash

    FIG='unique'
    awk <<ENDHER
        BEGIN      {i=0;step=4;}
        NR%step==0 {print $0>>${FIG}.i.dat;}
        /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
    ENDHER

It didn't work. The reason I want to use the "here" document is that I want to
pass the value of $FIG into the awk script. I know I can pass it as an option
to awk but just to make sure I don't miss this:

   Awk doesn't work with "here" document, does it?

Thanks very much.
Weiguang

--

Department of Computing Science           | http://www.*-*-*.com/ ~wgshi
University of Alberta                     | Office: ATH 134 (780)492-4006
Edmonton, AB, Canada, T6G 2E8             | (L) (780)492-7747



Sun, 14 Dec 2003 11:08:44 GMT  
 awk doesn't take "here" document?

Quote:
> I wrote something like this:

>     #!/bin/bash

>     FIG='unique'
>     awk <<ENDHER
>         BEGIN      {i=0;step=4;}
>         NR%step==0 {print $0>>${FIG}.i.dat;}
>         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
>     ENDHER

> It didn't work. The reason I want to use the "here" document is that I
want to
> pass the value of $FIG into the awk script. I know I can pass it as an
option
> to awk but just to make sure I don't miss this:

>    Awk doesn't work with "here" document, does it?

AWK does indeed work with "here" documents. However: The "here" document is
read as data *not* a script. Why do you not want to pass $FIG as an option?

Regards,
   =Adrian=



Sun, 14 Dec 2003 16:09:09 GMT  
 awk doesn't take "here" document?

Quote:

> I wrote something like this:

>     #!/bin/bash

>     FIG='unique'
>     awk <<ENDHER
>         BEGIN      {i=0;step=4;}
>         NR%step==0 {print $0>>${FIG}.i.dat;}
>         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
>     ENDHER

> It didn't work. The reason I want to use the "here" document is that I want to
> pass the value of $FIG into the awk script. I know I can pass it as an option
> to awk but just to make sure I don't miss this:

>    Awk doesn't work with "here" document, does it?

Since awk normally takes its input from stdin, reading the awk program
from stdin can't really work at the same time, can it?
It *can* be done if you don't need to read stdin:

awk -f - <<EOF
BEGIN {print "fish"}
EOF

should work. It won't work in your case though: even if you didn't
read stdin, it wouldn't be any better than enclosing the whole script
in double quotes, because you can't control which special characters
are interpreted by the shell and which by awk.

Passing the variable to awk script can be done easily enough without
that, however, e.g.,

     awk '
         BEGIN      {i=0;step=4;}
         NR%step==0 {print $0>>"'"${FIG}"'." i ".dat" }
         /segment/  { i++; step=0; printf "#segment%d\n", i>>"'"${FIG}"'." i ".dat" }'

or

     awk -v fig="${FIG}" '
         BEGIN      {i=0;step=4;}
         NR%step==0 {print $0>> fig "." i ".dat" }
         /segment/  { i++; step=0; printf "#segment%d\n", i>> fig "." i ".dat" }'

or

     awk '
         BEGIN      {i=0;step=4;}
         NR%step==0 {print $0>> fig "." i ".dat" }
         /segment/  { i++; step=0; printf "#segment%d\n", i>> fig "." i ".dat" }
        ' fig="${FIG}"

Pay attention to quotes.

--
Tapani Tarvainen



Sun, 14 Dec 2003 16:11:45 GMT  
 awk doesn't take "here" document?
FIG='unique'
awk -f - <<ENDHER
        BEGIN      { i=0;step=4; }
        NR%step==0 { print "$0">> "${FIG}." i ".dat"; }
        /segment/  { i++; step=0; printf "#segment%d\n", i >> "${FIG}." i ".dat";
Quote:
}

ENDHER


Sun, 14 Dec 2003 17:28:43 GMT  
 awk doesn't take "here" document?
Quote:

> Hi there,

> I wrote something like this:

>     #!/bin/bash

>     FIG='unique'
>     awk <<ENDHER

you can check I this gives what you expect by using this line instead:
cat << ENDHER >> qq.awk

Quote:
>         BEGIN      {i=0;step=4;}
>         NR%step==0 {print $0>>${FIG}.i.dat;}
>         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
>     ENDHER

and then check the file qq.awk
you'll probably find that:
- you have to escape $0 --> \$0
- ${FIG}.i.dat should be ${FIG} "." i ".dat"
- if step = 0 then NR % 0 gives an error message.

Quote:

> It didn't work. The reason I want to use the "here" document is that I want to
> pass the value of $FIG into the awk script. I know I can pass it as an option
> to awk but just to make sure I don't miss this:

>    Awk doesn't work with "here" document, does it?

I have no idea what you mean by (a) '"here" document'

goodluck,Eiso

Quote:

> Thanks very much.
> Weiguang

> --

> Department of Computing Science           | http://www.cs.ualberta.ca/~wgshi
> University of Alberta                     | Office: ATH 134 (780)492-4006
> Edmonton, AB, Canada, T6G 2E8             | (L) (780)492-7747

--
                                _________
_______________________________/ Eiso AB \_________________________


Sun, 14 Dec 2003 19:43:02 GMT  
 awk doesn't take "here" document?
Quote:


> > Hi there,

> > I wrote something like this:

> >     #!/bin/bash

> >     FIG='unique'
> >     awk <<ENDHER
> you can check I this gives what you expect by using this line instead:
> cat << ENDHER >> qq.awk

make that: cat << ENDHER > qq.awk
Eiso
Quote:

> >         BEGIN      {i=0;step=4;}
> >         NR%step==0 {print $0>>${FIG}.i.dat;}
> >         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
> >     ENDHER

> and then check the file qq.awk
> you'll probably find that:
> - you have to escape $0 --> \$0
> - ${FIG}.i.dat should be ${FIG} "." i ".dat"
> - if step = 0 then NR % 0 gives an error message.

> > It didn't work. The reason I want to use the "here" document is that I want to
> > pass the value of $FIG into the awk script. I know I can pass it as an option
> > to awk but just to make sure I don't miss this:

> >    Awk doesn't work with "here" document, does it?
> I have no idea what you mean by (a) '"here" document'

> goodluck,Eiso

> > Thanks very much.
> > Weiguang

> > --

> > Department of Computing Science           | http://www.cs.ualberta.ca/~wgshi
> > University of Alberta                     | Office: ATH 134 (780)492-4006
> > Edmonton, AB, Canada, T6G 2E8             | (L) (780)492-7747

> --

--
                                _________
_______________________________/ Eiso AB \_________________________


Sun, 14 Dec 2003 19:45:22 GMT  
 awk doesn't take "here" document?

% I wrote something like this:
%
%     #!/bin/bash
%    
%     FIG='unique'
%     awk <<ENDHER
%         BEGIN      {i=0;step=4;}
%         NR%step==0 {print $0>>${FIG}.i.dat;}
%         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
%     ENDHER

The normal way of doing this is to put the awk script in double quotes on the
command line:

     #!/bin/bash

     FIG='unique'
     awk "
         BEGIN      {i=0;step=4;}
         NR%step==0 {print $0>>${FIG}.i.dat;}
         /segment/  { i++; step=0; printf \"#segment%d\n\", i>>${FIG}.i.dat;}
     " inputfile?

But you can put it in a here document with the appropriate command-line:

     #!/bin/bash

     FIG='unique'
     awk -f - <<ENDHER inputfile?
         BEGIN      {i=0;step=4;}
         NR%step==0 {print $0>>${FIG}.i.dat;}
         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
     ENDHER

--

Patrick TJ McPhee
Knightsbridge SW7



Mon, 15 Dec 2003 00:40:01 GMT  
 awk doesn't take "here" document?

Quote:



>% I wrote something like this:
>%
>%     #!/bin/bash
>%
>%     FIG='unique'
>%     awk <<ENDHER
>%         BEGIN      {i=0;step=4;}
>%         NR%step==0 {print $0>>${FIG}.i.dat;}
>%         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
>%     ENDHER

>The normal way of doing this is to put the awk script in double quotes on the
>command line:

Single quotes.  You will cause yourself nothing but headaches if you get in
the habit of using double quotes with AWK scripts.  There are various
methods of getting shell variables into AWK scripts - check the FAQs.
(Or suggest that the best way to do it is to use ENVIRON, and watch Jim
Monty explode like the 4th of July...)

Re the original posters issues: There's not much difference between putting
your AWK program in a here-document vs. putting it on the command line -
either method can be quoted or not quoted, as you choose.  (By "quoted", I
mean setup so that ${...} and `...` are not expanded)  About the only time
you want to supply your AWK program on stdin is when the AWK program itself
is generated by a program (and even then, you could probably get away with
using `...`)



Mon, 15 Dec 2003 01:14:52 GMT  
 awk doesn't take "here" document?

Quote:

> Hi there,
> I wrote something like this:
>     #!/bin/bash

>     FIG='unique'
>     awk <<ENDHER
>         BEGIN      {i=0;step=4;}
>         NR%step==0 {print $0>>${FIG}.i.dat;}
>         /segment/  { i++; step=0; printf "#segment%d\n", i>>${FIG}.i.dat;}
>     ENDHER

        You'd use awk -f - <<ENDHER
        ... except that this would not work your
        input streams.

        To interpolate your shell variables into your awk
        You're better off with something like:

                #!/bin/bash
                FIG='unique'
                awk '
                        BEGIN  { step=4 }
                        NR % step { print $0>>'"$FIG"'.i.dat}
                        /segment/  { i++; step=0
                                     printf "#segment%d\n", i>>'"$FIG"'.i.dat
                }'

        ... BTW: you don't need to initial i=0 (all variables in awk
        are treated as having value 0 or null string (depending on
        context) upon first use.  You also don't need to use semicolons
        at the ends of lines nor before closing braces; they are simply
        there to separate commands that are on the same line.

        Notice that I use '"$FIG"' in the midst of this multi-line
        single quoted string.  The first ' in '"$FIG"' is a *CLOSE* quote;
        and the other one is to *re-open* the quote.  I don't need
        braces around the variable name (since it isn't adjacent to
        characters with create a parsing ambiguity for the shell;
        you'd only need that if you had something like:
           echo "this is a ${FIG}ly silly use of a non-$FIG variable"

        Also note that the double quotes around $FIG in my examples
        aren't necessary for this case but would be vital if $FIG's
        value had any embedded whitespace.  It's important that
        awk's entire "script" argument be represented as a single
        argument to it.  That means we can't have any unquoted
        whitspaces after the shell as performed it's variable
        and other forms of expansion.

Quote:
> It didn't work. The reason I want to use the "here" document is that I want to
> pass the value of $FIG into the awk script. I know I can pass it as an option
> to awk but just to make sure I don't miss this:

        You don't want to do this.  You're clobbering your
        file descriptors.

Quote:
>    Awk doesn't work with "here" document, does it?

        Sure it does.  

                awk -F: -f - /etc/passwd <<FOO
                        NF != 7 {print "EEEK!"}
                FOO

        ... here we can have our script on stdin because our awk
        process is going to read from a list of files (in this case
        just one; but it could have been more).

Quote:
> Thanks very much.
> Weiguang



Wed, 17 Dec 2003 16:09:16 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. When "Cancel" doesn't cancel

2. "Empty" statement doesn't work

3. printer doesn't "print window"

4. '"""' and linefeed characters

5. `exec tclsh "$0" "$@"' doesn't work on some /bin/sh

6. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

7. PostScript hack to "fix" dpANS document

8. STD - Yet another "interesting" document

9. "catch" doesn't catch "expr" syntax errors

10. "&" and Awk

11. awk "search and replace"

12. "Embedding" an awk script in bash

 

 
Powered by phpBB® Forum Software