From: Stu on
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
From: pk on
On Wed, 7 Jul 2010 10:29:10 -0700 (PDT)
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.

You don't need awk at all. With bash, you can do

IFS=\: read -r cpu_hh cpu_mm cpu_sec <<<"$cpu_time"

From: Janis Papanagnou on
On 07/07/10 19:29, 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
> (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

Are you ask for something like...

cpu_time=23:12:56
x=$( echo $cpu_time | awk -F: '{print "cpu_hh="$1" cpu_mm="$2" cpu_sec="$3}' )
eval $x
echo $cpu_hh
echo $cpu_mm
echo $cpu_sec

Or maybe a shell only version... (in ksh, zsh, but not in bash)

echo $cpu_time | IFS=: read cpu_hh cpu_mm cpu_sec
echo $cpu_hh
echo $cpu_mm
echo $cpu_sec

The same in bash...

echo $cpu_time | { IFS=: read cpu_hh cpu_mm cpu_sec
echo $cpu_hh
echo $cpu_mm
echo $cpu_sec
}


Janis
From: Janis Papanagnou on
On 07/07/10 19:38, pk wrote:
> On Wed, 7 Jul 2010 10:29:10 -0700 (PDT)
> 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.
>
> You don't need awk at all. With bash, you can do
>
> IFS=\: read -r cpu_hh cpu_mm cpu_sec <<<"$cpu_time"
>

You can do that also in other shells, like ksh and zsh, BTW.
(But I think <<< is non standard.)

Janis
From: Ben Bacarisse on
Stu <beefstu350(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.
 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: rm in a regular expression
Next: Pattern matching