Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messagemoonhk 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.
Post Follow-up to this messageOn 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
~
Post Follow-up to this messagemoonhk 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.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.