I want to put my awk file into ksh script 
Author Message
 I want to put my awk file into ksh script

Hi,

I have a awk file(myawk.awk) as follows:
{for (i = 1; i<=NF; i++) }
{
  if ($1 ~/^c/)
  {
    print "Device: " $1
    system("ls -l /dev/dsk/" $1 "s0 |nawk '{print $11}'| cut -c 14- |sed 's/:a//g'")
  }
  else
  if ($1 ~/^r/)
  {
   print "Device: " $1
   system("ls -l /dev/" $1 "|nawk '{print $11}' |cut -c 14- |sed 's/:$//g'")
  }
  if ($1 ~/Vendor/) print
  if ($1 ~/RPM/) print

Quote:
}

Presently I am running it in my Korn Shell script as
nawk -f /usr/local/bin/myawk.awk /tmp/file2.txt

How can I include this awk file into the my ksh script.

Thanks
Uman



Mon, 02 May 2005 09:57:31 GMT  
 I want to put my awk file into ksh script

Quote:

> Hi,

> I have a awk file(myawk.awk) as follows:
> {for (i = 1; i<=NF; i++) }
> {
>   if ($1 ~/^c/)
>   {
>     print "Device: " $1
>     system("ls -l /dev/dsk/" $1 "s0 |nawk '{print $11}'| cut -c 14- |sed 's/:a//g'")
>   }
>   else
>   if ($1 ~/^r/)
>   {
>    print "Device: " $1
>    system("ls -l /dev/" $1 "|nawk '{print $11}' |cut -c 14- |sed 's/:$//g'")
>   }
>   if ($1 ~/Vendor/) print
>   if ($1 ~/RPM/) print
> }
> Presently I am running it in my Korn Shell script as
> nawk -f /usr/local/bin/myawk.awk /tmp/file2.txt

> How can I include this awk file into the my ksh script.

First, there are a couple of points.

Are you sure this script works? In the loop, i is not used:
you do the same thing (to $1) NF times. Is that what you want?

You are probably better off calling it from your ksh script
like you do now. It will be easier to maintain or change if
you keep it separate.

The pipelines "nawk | cut | sed" could all be done in nawk.
Use sub() in place of sed and either sub() or substr() instead
of cut.

Now, your problem in incorporating it directly into your
ksh script is all those quotation marks. If you can get rid of
the single quotes in the pipeline you pass to system(), then
your ksh script just has:
awk -f '
    # your awk program
' /tmp/file2.txt

So, how to get rid of those pesky single quotes?
Easiest (and clearest, without escaping everything in
sight which just makes things hard to read and is
difficult to do) is to build up the pipeline command
using sprintf:
    SINQUOTE = 39
    pipeline = sprintf("whatever %c stuff in quotes %c whatever", SINQUOTE, SINQUOTE)
    system(pipeline)

Where does /tmp/file2.txt come from. If you do:
    myprog > /tmp/file2.txt
    awk ' your program' /tmp/file2.txt
then you could change this to:
    myprog | awk ' your program'

Finally, have I understood the question at all, or
are you wanting to get rid of awk altogether and
do the whole thing as a shellscript (without awk)?
This is certainly possible, but might be off-topic for
comp.lang.awk.

John.



Mon, 02 May 2005 18:08:09 GMT  
 I want to put my awk file into ksh script


Quote:
> > Hi,

> > I have a awk file(myawk.awk) as follows:
> > {for (i = 1; i<=NF; i++) }
> > {
> >   if ($1 ~/^c/)
> >   {
> >     print "Device: " $1
> >     system("ls -l /dev/dsk/" $1 "s0 |nawk '{print $11}'| cut -c 14- |sed
's/:a//g'")
> >   }
> >   else
> >   if ($1 ~/^r/)
> >   {
> >    print "Device: " $1
> >    system("ls -l /dev/" $1 "|nawk '{print $11}' |cut -c 14- |sed
's/:$//g'")
> >   }
> >   if ($1 ~/Vendor/) print
> >   if ($1 ~/RPM/) print
> > }
> > Presently I am running it in my Korn Shell script as
> > nawk -f /usr/local/bin/myawk.awk /tmp/file2.txt

> > How can I include this awk file into the my ksh script.

> First, there are a couple of points.

> Are you sure this script works? In the loop, i is not used:
> you do the same thing (to $1) NF times. Is that what you want?

> You are probably better off calling it from your ksh script
> like you do now. It will be easier to maintain or change if
> you keep it separate.

> The pipelines "nawk | cut | sed" could all be done in nawk.
> Use sub() in place of sed and either sub() or substr() instead
> of cut.

> Now, your problem in incorporating it directly into your
> ksh script is all those quotation marks. If you can get rid of
> the single quotes in the pipeline you pass to system(), then
> your ksh script just has:
> awk -f '
>     # your awk program
> ' /tmp/file2.txt

> So, how to get rid of those pesky single quotes?
> Easiest (and clearest, without escaping everything in
> sight which just makes things hard to read and is
> difficult to do) is to build up the pipeline command
> using sprintf:
>     SINQUOTE = 39
>     pipeline = sprintf("whatever %c stuff in quotes %c whatever",
SINQUOTE, SINQUOTE)
>     system(pipeline)

> Where does /tmp/file2.txt come from. If you do:
>     myprog > /tmp/file2.txt
>     awk ' your program' /tmp/file2.txt
> then you could change this to:
>     myprog | awk ' your program'

> Finally, have I understood the question at all, or
> are you wanting to get rid of awk altogether and
> do the whole thing as a shellscript (without awk)?
> This is certainly possible, but might be off-topic for
> comp.lang.awk.

> John.

Hi, John -

Concerning the analysis of the AWK, you say "Are you sure this script
works?"  I'm pretty sure it's not even syntactically correct, but it has an
issue that's a bit esoteric, I guess.  The for-loop is enclosed in braces --
so I can't see any argument that it would repreat the next braced section of
code "NF" times.  But it might repeat a null statement NF times.

    > > {for (i = 1; i<=NF; i++) }

Gnu AWK (my 3.1.0 at least) requires the null statement (that is the body of
the for-loop) to be delimited with a semicolon.  It actually won't run this
code.  It must be modified to

    {for (i = 1; i<=NF; i++);}

Otherwise it parses this as an unterminated statement.  I can see how nawks
authors might have decided to parse this syntax differently.  But I can't
imagine that they would not close the body of the loop at the closing brace,
and instead, treat the next action with the default pattern as a braced
statement (that is the body of the braced for-clause).

I think at the most, all this does is set i to NF+1, using a series of
increments.

Another note for the OP:  If, indeed, this code is only supposed to execute
once for each input record, not NF times for each record, then you have a
single-action program similar in form to:

    {
        if (cond1a)
            statement1a
        else if (cond1b)
            statement1b
        else
            statement1c
        if (cond2) statement2
        if (cond3) statement3
    }

This can usually be expressed more elegantly by using the conditions as
patterns in a multi-action program.  The leading if-else structure is
tricky, so I will decompose in two steps.

    {
        if (cond1a)
            statement1a
        else if (cond1b)
            statement1b
        else
            statement1c
    }
    cond2 {statement2}
    cond3 {statement3}

The next step is handling that else without breaking the code.  That is less
elegant.

    ta = cond1a {statement1a}
    !ta && (tb = cond1b) {statement1b}
    !ta && !tb {statement1c}
    cond2 {statement2}
    cond3 {statement3}

In your case, since cond1a is exclusive of cond1b, you don't need to worry
about making each clause exclusive...and, you don't have a general else, so
you don't need to worry about that, either.

    cond1a {statement1a}
    cond1b {statement1b}
    cond2 {statement2}
    cond3 {statement3}

statement1a and statement1b are compound statements, with the same first
statement, so you can do this:

    (ta = cond1a) || (tb = cond1b) {statement1common}
    ta {statement1aDiff}
    tb {statement1bDiff}
    cond2 {statement2}
    cond3 {statement3}

Since statement 2 and 3 are the same in your case,

    (ta = cond1a) || (tb = cond1b) {statement1common}
    ta {statement1aDiff}
    tb {statement1bDiff}
    cond2 || cond3 {statement2}

And since statement2 is {print}--the default action in AWK--the action can
be ommitted.

    (ta = cond1a) || (tb = cond1b) {statement1common}
    ta {statement1aDiff}
    tb {statement1bDiff}
    cond2 || cond3

Just substitute in your code chunks.  But many would argue this is much
harder to read, even though it's a lot shorter and more concise.

- Dan



Mon, 02 May 2005 23:01:44 GMT  
 I want to put my awk file into ksh script

...

Quote:
>Concerning the analysis of the AWK, you say "Are you sure this script
>works?"  I'm pretty sure it's not even syntactically correct, but it has an
>issue that's a bit esoteric, I guess.  The for-loop is enclosed in braces --
>so I can't see any argument that it would repreat the next braced section of
>code "NF" times.  But it might repeat a null statement NF times.

>    > > {for (i = 1; i<=NF; i++) }

>Gnu AWK (my 3.1.0 at least) requires the null statement (that is the body of
>the for-loop) to be delimited with a semicolon.  It actually won't run this
>code.  It must be modified to

>    {for (i = 1; i<=NF; i++);}

>Otherwise it parses this as an unterminated statement.  I can see how nawks
>authors might have decided to parse this syntax differently.  But I can't
>imagine that they would not close the body of the loop at the closing brace,
>and instead, treat the next action with the default pattern as a braced
>statement (that is the body of the braced for-clause).

FWIW, both gawk and tawk barf on this construct, but both Solaris nawk and
mawk are OK with it.

Here's a demo with mawk:

 % mawk 'function foo() {print "hi";return 1};BEGIN{for (i=0; i<5; i+=foo())}'
 hi
 hi
 hi
 hi
 hi
 %



Mon, 02 May 2005 23:20:54 GMT  
 I want to put my awk file into ksh script
Hi,
I agree that my for loop is irrelevent to this script, but the script
works fine. Anyhow I found the answer from another mailing list.
Thanks to everyone.
My ksh script will embed the follwing line:
nawk '
$1 ~ /^c/ {
                print "Device: " $1
                cmd = "ls -l /dev/dsk/" $1 "s0"
                while (cmd | getline > 0) {
                        tmp = substr($11, 14)
                        gsub(/:a/, "", tmp)
                        print tmp
                }
                close(cmd)
                next
        }
$1 ~ /^r/ {
                cmd = "ls -l /dev/" $1
                while (cmd | getline > 0) {
                       tmp = substr($11, 14)
                       gsub(/:/, "", tmp)
                       print tmp
                }
                close(cmd)
                next
        }
        $1 ~ /Vendor/ { print; next }
        $1 ~ /RPM/ { print }
' /myfile.txt
Quote:



> ...
> >Concerning the analysis of the AWK, you say "Are you sure this script
> >works?"  I'm pretty sure it's not even syntactically correct, but it has an
> >issue that's a bit esoteric, I guess.  The for-loop is enclosed in braces --
> >so I can't see any argument that it would repreat the next braced section of
> >code "NF" times.  But it might repeat a null statement NF times.

> >    > > {for (i = 1; i<=NF; i++) }

> >Gnu AWK (my 3.1.0 at least) requires the null statement (that is the body of
> >the for-loop) to be delimited with a semicolon.  It actually won't run this
> >code.  It must be modified to

> >    {for (i = 1; i<=NF; i++);}

> >Otherwise it parses this as an unterminated statement.  I can see how nawks
> >authors might have decided to parse this syntax differently.  But I can't
> >imagine that they would not close the body of the loop at the closing brace,
> >and instead, treat the next action with the default pattern as a braced
> >statement (that is the body of the braced for-clause).

> FWIW, both gawk and tawk barf on this construct, but both Solaris nawk and
> mawk are OK with it.

> Here's a demo with mawk:

>  % mawk 'function foo() {print "hi";return 1};BEGIN{for (i=0; i<5; i+=foo())}'
>  hi
>  hi
>  hi
>  hi
>  hi
>  %



Tue, 03 May 2005 09:10:12 GMT  
 I want to put my awk file into ksh script

Quote:

> Hi,
> I agree that my for loop is irrelevent to this script, but the script
> works fine. Anyhow I found the answer from another mailing list.
> Thanks to everyone.
> My ksh script will embed the follwing line:
> nawk '
> $1 ~ /^c/ {
>                 print "Device: " $1
>                 cmd = "ls -l /dev/dsk/" $1 "s0"
>                 while (cmd | getline > 0) {
>                         tmp = substr($11, 14)
>                         gsub(/:a/, "", tmp)
>                         print tmp
>                 }
>                 close(cmd)
>                 next
>         }
> $1 ~ /^r/ {
>                 cmd = "ls -l /dev/" $1
>                 while (cmd | getline > 0) {
>                        tmp = substr($11, 14)
>                        gsub(/:/, "", tmp)
>                        print tmp
>                 }
>                 close(cmd)
>                 next
>         }
>         $1 ~ /Vendor/ { print; next }
>         $1 ~ /RPM/ { print }
> ' /myfile.txt

Note that there is a subtle difference from your original version.
The statement gsub(/:/, "", tmp) will remove all colons, whereas your
sed 's/:$//g' took out only the last one (sed's g was redundant).

And maybe the variable name tmp (which normally means temporary)
could be replaced with something more informative.

John.



Tue, 03 May 2005 14:58:09 GMT  
 I want to put my awk file into ksh script
On 13 Nov 2002 17:57:31 -0800,

Quote:
> Presently I am running it in my Korn Shell script as
> nawk -f /usr/local/bin/myawk.awk /tmp/file2.txt

> How can I include this awk file into the my ksh script.

You could put "#!/usr/bin/nawk -f" as the first line of the awk script,
and then run it as "myawk.awk /tmp/file2.txt"


Wed, 04 May 2005 00:22:31 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. awk within ksh script

2. ksh-awk script problem

3. scripts: ksh calls awk: no stdout?

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

5. Getting awk (or sed or anything else) to put single quotes into file

6. variabe in variable like in ksh in awk?

7. awk in ksh

8. Perl versus sed + awk + ksh

9. expect script with puts to file

10. removing shell comments from ksh scripts

11. Fortran output and ksh scripts

12. running python script on ksh on NT

 

 
Powered by phpBB® Forum Software