From: pk on
On Wed, 07 Jul 2010 19:57:33 +0200 Janis Papanagnou
<janis_papanagnou(a)hotmail.com> wrote:

> > 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.)

It's not, that's what I say "in bash", because it's the only one I know
that does it. But yes, obviously it is probably supported by other shells.
And with shells that don't run the last command in a subshell, one can even
do (standard)

echo "$cpu_time" | read ...

From: steven_nospam at Yahoo! Canada on
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


From: Janis Papanagnou on
On 07/07/10 19:49, pk wrote:
> On Wed, 07 Jul 2010 19:57:33 +0200 Janis Papanagnou
> <janis_papanagnou(a)hotmail.com> wrote:
>
>>> 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.)
>
> It's not, that's what I say "in bash", because it's the only one I know
> that does it. But yes, obviously it is probably supported by other shells.

Quite some modern shell features in bash have been borrowed from other
shells; so it's always worth to check that if one makes a statement
about a presumed bash'ism. Though, bash has also a few proprietary
constructs re-invented unnecessarily, that other shells already had
available in a different way.

> And with shells that don't run the last command in a subshell, one can even
> do (standard)
>
> echo "$cpu_time" | read ...
>

Yes, that's what I proposed, and you can do it even with shells that
run the last pipe copmmand in a subshell if you use the command
grouping construct that I posted as variant.

Janis
From: Janis Papanagnou on
On 07/07/10 21:48, 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

Since you can simply define the IFS for the read command there's no
need to require the restriction of white space field separation in
the input data. (See pk's, my own, and Ben's posting upthread.)

The bigger problem is the subshell incoherence acroll various shells
WRT the last pipe command; but there's as well a simple construct to
address that.

Janis
From: Janis Papanagnou on
On 08/07/10 00:15, Janis Papanagnou wrote:
> [...]
>
> The bigger problem is the subshell incoherence acroll various shells

s/acroll/across/

> WRT the last pipe command; but there's as well a simple construct to
> address that.
>
> Janis

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