From: Icarus Sparry on
On Wed, 07 Jul 2010 12:48:45 -0700, steven_nospam at Yahoo! Canada wrote:

> On Jul 7, 1:29 pm, Stu <beefstu...(a)hotmail.com> wrote:
>> I want to parse HH:MM:SS with AWK.
>>
>> I know I can do something like this (see below) and this will put my
>> values into
>> variables, which I can use in the shell. But it also involves invoking
>> awk 3 times,
>> which is wasteful.
>>
>> cpu_hh=$(echo $cpu_time | $AWK -F':' ' { print $1 } ') cpu_mm=$(echo
>> $cpu_time | $AWK -F':' ' { print $2 } ') cpu_sec=$(echo $cpu_time |
>> $AWK -F':' ' { print $3 } ')
>>
>> can somebody show me an example of how I can pass a variable into awk
>> (in this case I am assuming I have to initialize it as an array set -A
>> array before
>> I pass it into awk?), use the split command, and have the values
>> available in my
>> variable I passed in  Ie  arrary[1], array[2], array[3] so I  can
>> access the variable
>> outside of AWK but within my script.
>>
>> Thanks to all who answer this post
>
> Several good answers have been supplied. Another alternative is (if this
> is just a time value obtained from the "date" command) is to alter the
> usage of date so that you don't have to strip out the colon or alter the
> field separator. This works in Korn shell:
>
> # echo "$(date +"%H %M %S")" | read cpu_hh cpu_mm cpu_sec

No idea why you are using echo here, a straight forward

date +"%H %M %S" | read cpu_hh cpu_mm cpu_sec

works fine and should be faster. (Note other comments about bash not
running the final process in a pipeline in the current shell....)

However

eval $(date +"cpu_hh=%H; cpu_mm=%M; cpu_sec=%S" )

works fine in all modern "sh" shells.


From: Barry Margolin on
In article
<1f59e949-ae8d-4edf-9e30-952927bd51ba(a)k39g2000yqd.googlegroups.com>,
Stu <beefstu350(a)hotmail.com> wrote:

> I want to parse HH:MM:SS with AWK.
>
> I know I can do something like this (see below) and this will put my
> values into
> variables, which I can use in the shell. But it also involves invoking
> awk 3 times,
> which is wasteful.
>
> cpu_hh=$(echo $cpu_time | $AWK -F':' ' { print $1 } ')
> cpu_mm=$(echo $cpu_time | $AWK -F':' ' { print $2 } ')
> cpu_sec=$(echo $cpu_time | $AWK -F':' ' { print $3 } ')
>
> can somebody show me an example of how I can pass a variable into awk
> (in this case I am assuming I have to initialize it as an array set -A
> array before
> I pass it into awk?), use the split command, and have the values
> available in my
> variable I passed in Ie arrary[1], array[2], array[3] so I can
> access the variable
> outside of AWK but within my script.
>
> Thanks to all who answer this post

Others have shown how to do this without awk. But it often IS useful to
pass variables into awk, I thought I'd answer that question. You do it
using the -v option, e.g.

awk -v cpu_time=$cpu_time 'BEGIN {split(cpu_time, array, ":"); print
array[1], array[2], array[3]}'

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Ed Morton on
On 7/7/2010 12:29 PM, Stu wrote:
> I want to parse HH:MM:SS with AWK.
>
> I know I can do something like this (see below) and this will put my
> values into
> variables, which I can use in the shell. But it also involves invoking
> awk 3 times,
> which is wasteful.
>
> cpu_hh=$(echo $cpu_time | $AWK -F':' ' { print $1 } ')
> cpu_mm=$(echo $cpu_time | $AWK -F':' ' { print $2 } ')
> cpu_sec=$(echo $cpu_time | $AWK -F':' ' { print $3 } ')
>
> can somebody show me an example of how I can pass a variable into awk

You CANNOT, see below.

> (in this case I am assuming I have to initialize it as an array set -A
> array before
> I pass it into awk?), use the split command, and have the values
> available in my
> variable I passed in Ie arrary[1], array[2], array[3] so I can
> access the variable
> outside of AWK but within my script.

Others have answered your question, but the above makes me think you may have a
fundamental misunderstanding about the relationship between awk and shell. awk
is not shell. You can no more pass a shell array to awk than you can pass a
shell array to a C program. Ditto for any variable. You wouldn't expect to pass
a shell variable to a C program and after the C program runs the shell variable
to have magically changed value, and neither should you expect an awk program to
be able to alter the value of a shell variable.

You can pass the VALUE of a shell variable (i.e. not the variable, but it's
contents) to a C program and you can have that C program print something in it's
output and have the shell assign that original variable to the value of that
printed output, e.g.:

sh_var=$(c_prog "$sh_var")

and so can you with awk:

sh_var=$(awk 'awk_script' "$sh_var")

but in awk there's a shorthand that lets you specify the name of an awk variable
to initialize with that shell variables value on the command line:

sh_var=$(awk -v awk_var="$sh_var" 'awk_script')

rather than just passing in a value and having awk_script figure out which
variable to initialize to that value.

So, if you want to write an awk script that takes the value of some shell
variable and print it's value divided by 2, that'd be:

sh_var=10
awk -v awk_var="$sh_var" 'BEGIN{ print awk_var / 2; exit }'

and if you want to set the shell variable to that result, then it's:

sh_var=10
sh_var=$(awk -v awk_var="$sh_var" 'BEGIN{ print awk_var / 2; exit }')

but be clear - you are passing in the VALUE of a shell variable to awk and
printing the result of the awk script and then setting the shell variable to
awks output, you are NOT passing in and manipulating a shell variable.

Ed.

P.S. Yes, I know you can write a script that jumps back and forth between awk
and shell using combinations of multiple embedded single and double quotes so it
looks like your awk script is directly accessing your shell variables. Just
don't - it's hard to read and error prone compared to doing it as above.

>
> Thanks to all who answer this post

From: Stu on
On Jul 7, 1:57 pm, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:
> Stu <beefstu...(a)hotmail.com> writes:
> > I want to parse HH:MM:SS with AWK.
>
> > I know I can do something like this (see below) and this will put my
> > values into
> > variables, which I can use in the shell. But it also involves invoking
> > awk 3 times,
> > which is wasteful.
>
> > cpu_hh=$(echo $cpu_time | $AWK -F':' ' { print $1 } ')
> > cpu_mm=$(echo $cpu_time | $AWK -F':' ' { print $2 } ')
> > cpu_sec=$(echo $cpu_time | $AWK -F':' ' { print $3 } ')
>
> > can somebody show me an example of how I can pass a variable into awk
> > (in this case I am assuming I have to initialize it as an array set -A
> > array before
> > I pass it into awk?), use the split command, and have the values
> > available in my
> > variable I passed in  Ie  arrary[1], array[2], array[3] so I  can
> > access the variable
> > outside of AWK but within my script.
>
> I think you are asking the wrong question.  Why must you use awk?  You
> can get the result directly into shell variables in lots of ways that
> don't use awk.  For example
>
>   IFS=':' read cpu_hh cpu_mm cpu_ss
>
> and provide the time as an input.  You could also, in bash, use an
> array:
>
>   cpu=($(tr ':' ' ' <<<$cpu_time))
>
> to get ${cpu[0]} etc.
>
> If you don't want to run even tr, use a function:
>
>   function split
>   {
>       IFS=':'
>       set "$1"
>       echo "$1" "$2" "$3"
>   }
>
>   cpu=($(split "$cpu_time"))
>
> Variations on this scheme could provide you with the components one at a
> time.
>
> In short, there are lots of ways that are probably better than using
> awk.  Which is best probably depends on where the data is coming from
> and where it will eventually go.  In some cases, we might go full circle
> and decide that awk is the best option if the data comes from a
> multi-line file with lot of times in it.
>
> --
> Ben.- Hide quoted text -
>
> - Show quoted text -

Ben thanks but your examples dont apper to work in ksh, which is the
shell I am using.

Ie

function split
{
IFS=':'
set "$1"
echo "$1" "$2" "$3"
}

cpu=($(split "$cpu_time"))

../split.ksht[42]: 0403-057 Syntax error at line 42 : `(' is not
expected.

As for this
cpu=($(tr ':' ' ' <<<$cpu_time))

note sure what <<< means as oppossed to < which I know what that means
so I am staying away
from this example. Also it does not work with ksh

../split.ksht[35]: 0403-057 Syntax error at line 35 : `(' is not
expected.

From: Janis Papanagnou on
On 09/07/10 16:47, Stu wrote:
> On Jul 7, 1:57 pm, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:
>> Stu <beefstu...(a)hotmail.com> writes:
>>> I want to parse HH:MM:SS with AWK.
>>
>>> I know I can do something like this (see below) and this will put my
>>> values into
>>> variables, which I can use in the shell. But it also involves invoking
>>> awk 3 times,
>>> which is wasteful.
>>
>>> cpu_hh=$(echo $cpu_time | $AWK -F':' ' { print $1 } ')
>>> cpu_mm=$(echo $cpu_time | $AWK -F':' ' { print $2 } ')
>>> cpu_sec=$(echo $cpu_time | $AWK -F':' ' { print $3 } ')
>>
>>> can somebody show me an example of how I can pass a variable into awk
>>> (in this case I am assuming I have to initialize it as an array set -A
>>> array before
>>> I pass it into awk?), use the split command, and have the values
>>> available in my
>>> variable I passed in Ie arrary[1], array[2], array[3] so I can
>>> access the variable
>>> outside of AWK but within my script.
>>
>> I think you are asking the wrong question. Why must you use awk? You
>> can get the result directly into shell variables in lots of ways that
>> don't use awk. For example
>>
>> IFS=':' read cpu_hh cpu_mm cpu_ss
>>
>> and provide the time as an input. You could also, in bash, use an
>> array:
>>
>> cpu=($(tr ':' ' ' <<<$cpu_time))
>>
>> to get ${cpu[0]} etc.
>>
>> If you don't want to run even tr, use a function:
>>
>> function split
>> {
>> IFS=':'
>> set "$1"
>> echo "$1" "$2" "$3"
>> }
>>
>> cpu=($(split "$cpu_time"))
>>
>> Variations on this scheme could provide you with the components one at a
>> time.
>>
>> In short, there are lots of ways that are probably better than using
>> awk. Which is best probably depends on where the data is coming from
>> and where it will eventually go. In some cases, we might go full circle
>> and decide that awk is the best option if the data comes from a
>> multi-line file with lot of times in it.
>>
>> --
>> Ben.- Hide quoted text -
>>
>> - Show quoted text -
>
> Ben thanks but your examples dont apper to work in ksh, which is the
> shell I am using.

I suppose you are probably using the PDksh, or maybe some very old ksh88?
Instead, use the one from AT&T (www.kornshell.com). The proposal works well
there.

But for your task I'd suggest to resort to Icarus' last suggestion which is
most simple and portable.

Janis

>
> Ie
>
> function split
> {
> IFS=':'
> set "$1"
> echo "$1" "$2" "$3"
> }
>
> cpu=($(split "$cpu_time"))
>
> ./split.ksht[42]: 0403-057 Syntax error at line 42 : `(' is not
> expected.
>
> As for this
> cpu=($(tr ':' ' ' <<<$cpu_time))
>
> note sure what <<< means as oppossed to < which I know what that means
> so I am staying away
> from this example. Also it does not work with ksh
>
> ./split.ksht[35]: 0403-057 Syntax error at line 35 : `(' is not
> expected.
>

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5
Prev: rm in a regular expression
Next: Pattern matching