how do i assign data from an awk to a variable on the script, since my script is using bourne and awk

how do i assign data from an awk to a variable on the script, since my script is using bourne and awk

Post by Denni » Mon, 16 Apr 2001 12:33:47



I am using awk in my script, I wanted to assign data from awk to a local
variable on the script. How would I do that. This is my script:

#! /bin/sh

filedate=0
monthname="Jan"
ax=`ls -al|awk '{if ($6 == '$monthname') { print $6, $7}}`

I want to assign the data in $7 to the variable filedate, so it would be
available to perform manipulations from the script, outside the awk. I tried
doing " filedate = $7 " it seems this only assigns it to a variable in the
awk.
Thanks

 
 
 

how do i assign data from an awk to a variable on the script, since my script is using bourne and awk

Post by t.. » Mon, 16 Apr 2001 14:07:28



> I am using awk in my script, I wanted to assign data from awk to a local
> variable on the script. How would I do that.

You can't do that directly: shell variables cannot be changed by
subprocesses.
Basically you have two options:
(1) write the value to a file and read it back from there;
(2) capture stdout from the subprocess and pick the result from there.

A third alternative that's sometimes possible is to reorganize
your script so that you call shell from within awk.

You already have an idea of a way to do the latter:

Quote:> ax=`ls -al|awk '{if ($6 == '$monthname') { print $6, $7}}`

although your quotes are a bit off, what you probably mean is

ax=`ls -al|awk '{if ($6 == "'$monthname'") { print $6, $7}}'`

or simpler

ax=`ls -al|awk '$6 == "'$monthname'" { print $6, $7}'`

Quote:> I want to assign the data in $7 to the variable filedate, so it would be
> available to perform manipulations from the script, outside the awk.

After the above you'd have months and days of month in variable ax,
and you could extract the desired data from there.
You'd have the dates for every such file in there though, and the month
for each is same so it doesn't really sound useful;
a while-read loop would probably be better.
But it depends on what you are trying to achieve. Perhaps you
could explain that a bit?

--
Tapani Tarvainen

 
 
 

how do i assign data from an awk to a variable on the script, since my script is using bourne and awk

Post by Juha Laih » Tue, 17 Apr 2001 05:54:07



Quote:>I am using awk in my script, I wanted to assign data from awk to a local
>variable on the script. How would I do that. This is my script:

>#! /bin/sh

>filedate=0
>monthname="Jan"
>ax=`ls -al|awk '{if ($6 == '$monthname') { print $6, $7}}`

>I want to assign the data in $7 to the variable filedate, so it would be
>available to perform manipulations from the script, outside the awk. I tried
>doing " filedate = $7 " it seems this only assigns it to a variable in the
>awk.

What you have above is close; there you get to shellvar ax contents of both
$6 and $7; note though that the ax might contain multiple lines, if the
directory listing has multiple files matching to the specified month.

One way to get multiple variables from awk back yo shell is to run the
awk within eval statement, like
eval `ls -al|awk '{if ($6 == "'$monthname'") { print "filedate=$7"; } }'`
... this will make the awk print out something that the shell can
recognise as commands, and when those command are passed as arguments
to eval (via the `...` structure), they will get executed by the calling
shell.

It is possible to have the awk program generate even largish pieces of
shell programs to be run via the eval statement -- the use is certainly
not limited to just setting variables. This can be needed at places if
it gets impossible to get plain shell code dynamic enough, but for
reason or another (typically political..) Perl cannot be used.

 
 
 

1. Question about using AWK in a BOURNE shell script

I have an awk program file that I need to incorporate into a bourne shell
script, to do some text formatting.

From this section of code:

#!/bin/sh

do
   cat ${file} | awk '
BEGIN {
       columns = 100;
       rows    = 80;
       page   = (rows - 66)
       top    = (page / 2)
       spaces = ((columns - 80)/2)
       for ( i = 0;i < top;i++ )
          print "";
      }
{
 if ( count >= 66 )
   {
    for ( i = 0;i < page;i++ )
      print "";
    count = 0;
   }
 for ( i=0;i < spaces;i++ )
   printf(" ");
 print $0;
 ++count;
done

I get the ERROR:  syntax error at line 12: `(' unexpected

I remedy this error by placing "'s before and after the ''s, but now I get
the ERROR: awk: syntax error near line 1
           awk: bailing out near line 1

Although this program works fine when it is in its own file and I issue the
command: awk -f program filename it will not work in the script.

Could someone tell me how to make awk accept my program in my shell script?

Christopher Walton

2. Pasting data in 'elvis' (vi clone)

3. how can I assign a data to a local variable from awk

4. Multiple Root masters with NIS+

5. Am I using old-AWK or new-AWK?

6. NFS port monitering

7. Using symbol (variable) by awk inside script

8. M$ NT Campain and WINE Pages

9. BASH $variable to AWK $variable in script?

10. Bourne shell script, Xterm and awk

11. sh & awk: Quoting Awk Syntax in Bourne shell?

12. interpeting variables in awk within a script

13. awk and sh variables in a shell script