How to run awk script 
Author Message
 How to run awk script

How do you run an awk script like a shell script? If I type something like:

ls -l | awk -f lpfilter

then it works just fine and does what I want it to. But I would like to be
able to type something like:

ls -l | lpfilter

As it stands now, if I type the latter, I get an error message from awk
saying the "filename=-". It doesn't seem to be taking standard input when
run that way.

I need it to run this way because I want to use it as an lp filter, and
list it on the :if= line in /etc/printcap.

Any ideas appreciated.
Thanks,

Here's the script:

#!/usr/bin/awk
        # Add printer setup codes to first long line
        # \x1B - ESC (ASCII 027, HEX 1B)
        # &l1o5.369C - orientation landscape, vertical motion index
        # (s12.5H - pitch 12.5
        # first part of P sets font to courier, pitch 10, symbol set PC-8
        # &l0o7.183C - orientation portrait, vertical motion index
        # %s - the string (the original line, $0)
        # \n - newline
BEGIN { W = 0
        I = "\x1B&l1o5.369C\x1B(s12.5H"
        P = "\x1B(10U\x1B(s0p10h0s0b4099T\x1B&l0o7.183C"
        }
{ if (W == 0 && length > 85) {
        if (index($0,"\x0C") != 0) {
                split($0,p,"\x0C")
                printf("%s\x0C%s%s\n",p[1],I,p[2])
        }
        else { printf("%s%s\n",I,$0) }
        W = 1
        }
else {
        { if (NR == 1) {
                printf("%s%s\n",P,$0)
                }
        else { print $0 }
        }
        }
        }



Tue, 21 Mar 2000 03:00:00 GMT  
 How to run awk script


Quote:

>How do you run an awk script like a shell script? If I type something like:

>ls -l | awk -f lpfilter

>then it works just fine and does what I want it to. But I would like to be
>able to type something like:

>ls -l | lpfilter

>As it stands now, if I type the latter, I get an error message from awk
>saying the "filename=-". It doesn't seem to be taking standard input when
>run that way.

>I need it to run this way because I want to use it as an lp filter, and
>list it on the :if= line in /etc/printcap.

>Any ideas appreciated.
>Thanks,

>Here's the script:

>#!/usr/bin/awk
>    # Add printer setup codes to first long line
>    # \x1B - ESC (ASCII 027, HEX 1B)
>    # &l1o5.369C - orientation landscape, vertical motion index
>    # (s12.5H - pitch 12.5
>    # first part of P sets font to courier, pitch 10, symbol set PC-8
>    # &l0o7.183C - orientation portrait, vertical motion index
>    # %s - the string (the original line, $0)
>    # \n - newline
>BEGIN { W = 0
>    I = "\x1B&l1o5.369C\x1B(s12.5H"
>    P = "\x1B(10U\x1B(s0p10h0s0b4099T\x1B&l0o7.183C"
>    }
>{ if (W == 0 && length > 85) {
>    if (index($0,"\x0C") != 0) {
>            split($0,p,"\x0C")
>            printf("%s\x0C%s%s\n",p[1],I,p[2])
>    }
>    else { printf("%s%s\n",I,$0) }
>    W = 1
>    }
>else {
>    { if (NR == 1) {
>            printf("%s%s\n",P,$0)
>            }
>    else { print $0 }
>    }
>    }
>    }

Make your first line:
#!/usr/bin/awk -f

and it should work.

Brian Bury



Tue, 21 Mar 2000 03:00:00 GMT  
 How to run awk script

On 3 Oct 97 12:47:07 GMT,

Quote:

> How do you run an awk script like a shell script? [...] I would like to be
> able to type something like:

> ls -l | lpfilter
> #!/usr/bin/awk

Make this

    #!/usr/bin/awk -f

Explanation:

When an executable file begins with the two bytes <pound><bang>, the OS
will know that it's supposed to start up the interpreter specified after
those two bytes, possibly followed by (usually limited to) one cluster
of command-line switches, and often limited to something like 32
characters.  When your file /myfile/ contains

    #!/path/to/interpreter -switches

as the first line and you start it up as

    myfile -args moreargs

the process started by the OS is in fact

    /path/to/interpreter -switches myfile -args moreargs

this is quite general, so you can have self-replicating scipts:

    #!/bin/cat
    This script cats itself (and any filename arguments) to stdout

or self-listing scripts (as in file listing):

    #!/bin/ls -laF
    The content of this script will not make much difference.

or self-removing scripts:

    #!/bin/rm -f
    This script will be removed whenever it's run

or, since awk likes a -f scriptfile argument, you can have

    #!/usr/bin/awk -f
    # This file will be taken to be an awk script when run.

which I think is what you want.

HTH,

--



Tue, 21 Mar 2000 03:00:00 GMT  
 How to run awk script

Quote:

>How do you run an awk script like a shell script? If I type something like:

>ls -l | awk -f lpfilter

>then it works just fine and does what I want it to. But I would like to be
>able to type something like:

>ls -l | lpfilter

Try the following at the top of your script:

 #!/usr/bin/awk -f

---

Ed Shannonhouse



Wed, 22 Mar 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. getting pid in running awk script

2. Run awk from within sed script?

3. Running Awk scripts from within VB

4. multiple concurrent runs of same K/awk script

5. run awk output thru tee inside awk

6. Creating an awk script to extract other scripts from a file

7. Access to Script Name Within Awk Script

8. accessing calling scripts from called scripts at run time

9. Running a TK script using wish80.exe within a tcl script

10. Running scripts within scripts

11. Replacing a string from an input file within awk script

12. Awk-Script for Text Analysis

 

 
Powered by phpBB® Forum Software