For Programmers: Free Programming Magazines  


Home > Archive > AWK > August 2007 > how to pass value to awk with space and ":" ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to pass value to awk with space and ":" ?
moonhk

2007-08-22, 3:57 am

Hi All

Do you know how to pass value to awk with space and ":" ?

When TIME=`date +%Y/%m/%d %H:%M:%S` not work

PROCESS()
{
TIME=`date +%Y/%m/%d.%H.%M.%S`
ps -e -o pid,ppid,pcpu,cpu,etime,time,vsz,user,rs
size,comm | \
sort -d -r -k 4 |awk -v T=$TIME '
BEGIN {
}
{
if ($4 ~ /^[0-9].*/ && $4 > 1) {
printf("%10s%10s%6s%6s%12s%10s%10s%10s%10s %-10s%10s\n",
$1,$2,$3,$4,$5,$6,$7,$8,$9,$10 , T)
}
} '
} # <-- end of PROCESS
#
# main
#
PROCESS
sleep 5
PROCESS

Ed Morton

2007-08-22, 3:57 am

moonhk wrote:
> Hi All
>
> Do you know how to pass value to awk with space and ":" ?
>
> When TIME=`date +%Y/%m/%d %H:%M:%S` not work
>
> PROCESS()
> {
> TIME=`date +%Y/%m/%d.%H.%M.%S`
> ps -e -o pid,ppid,pcpu,cpu,etime,time,vsz,user,rs
size,comm | \
> sort -d -r -k 4 |awk -v T=$TIME '
> BEGIN {
> }
> {
> if ($4 ~ /^[0-9].*/ && $4 > 1) {
> printf("%10s%10s%6s%6s%12s%10s%10s%10s%10s %-10s%10s\n",
> $1,$2,$3,$4,$5,$6,$7,$8,$9,$10 , T)
> }
> } '
> } # <-- end of PROCESS
> #
> # main
> #
> PROCESS
> sleep 5
> PROCESS
>


That's a shell question not an awk question, but the answer is to quote
your variables. Post to comp.unix.shell if you want more details.
Alternatively, use GNU awk's builtin time functions so you don't need to
invoke date externally.

As for the awk part, the BEGIN section should be removed, the condition
should be moved out of the action section, and this:

$4 ~ /^[0-9].*/

is equivalent to:

$4 ~ /^[0-9]/

when I think what you probably really meant was:

$4 ~ /^[0-9]+$/

so, something like this is what I think you were trying to do:

.... | awk '
BEGIN{ T=strftime("%Y/%m/%d.%H.%M.%S") }
$4 ~ /^[1-9][0-9]+*$/ {
printf("%10s%10s%6s%6s%12s%10s%10s%10s%10s %-10s%10s\n",
$1,$2,$3,$4,$5,$6,$7,$8,$9,$10 , T)
}'

Regards,

Ed.
moonhk

2007-08-23, 3:57 am

On 8 22 , 12 35 , Ed Morton <mor...@lsupcaemnt.com> wrote:
> moonhk wrote:
>
>
>
>
> That's a shell question not an awk question, but the answer is to quote
> your variables. Post to comp.unix.shell if you want more details.
> Alternatively, use GNU awk's builtin time functions so you don't need to
> invoke date externally.
>
> As for the awk part, the BEGIN section should be removed, the condition
> should be moved out of the action section, and this:
>
> $4 ~ /^[0-9].*/
>
> is equivalent to:
>
> $4 ~ /^[0-9]/
>
> when I think what you probably really meant was:
>
> $4 ~ /^[0-9]+$/
>
> so, something like this is what I think you were trying to do:
>
> ... | awk '
> BEGIN{ T=strftime("%Y/%m/%d.%H.%M.%S") }
> $4 ~ /^[1-9][0-9]+*$/ {
> printf("%10s%10s%6s%6s%12s%10s%10s%10s%10s %-10s%10s\n",
> $1,$2,$3,$4,$5,$6,$7,$8,$9,$10 , T)
>
> }'
>
> Regards,
>
> Ed.- -
>
> - -


Thank. But still can not pass Space. Now ":" ok


^C[shk:/home/ericl6/log/idle] ps1.ksh
4587554 4022482 0.0 54 00:00 00:00:00 1804
ericl6 1872 ps 2007/08/23.14:42:51


PROCESS()
{
TIME=`date +%Y/%m/%d.%H:%M:%S`
ps -e -o pid,ppid,pcpu,cpu,etime,time,vsz,user,rs
size,comm | \
sort -d -r -k 4 |awk -v T="$TIME" '
BEGIN {
}
{
if ($4 ~ /^[0-9].*/ && $4 > 1) {
printf("%10s%10s%6s%6s%12s%10s%10s%10s%10s %-10s%10s\n",
$1,$2,$3,$4,$5,$6,$7,$8,$9,$10 , T)
}
} '
} # <-- end of PROCESS
#
# main
#
PROCESS
sleep 5
PROCESS
~


Ed Morton

2007-08-23, 7:57 am

moonhk wrote:
> On 8 22 , 12 35 , Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> Thank. But still can not pass Space. Now ":" ok


":" was never a problem and spaces aren't a problem when you quote your
variable as I showed. So, you're going to have to post a snapshot of
whatever failure you're seeing to get more help. Also, see what I posted
previously for how to clean up your awk script.

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com