Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

how to pass value to awk with space and ":" ?
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


Report this thread to moderator Post Follow-up to this message
Old Post
moonhk
08-22-07 08:57 AM


Re: how to pass value to awk with space and ":" ?
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
08-22-07 08:57 AM


Re: how to pass value to awk with space and ":" ?
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
~



Report this thread to moderator Post Follow-up to this message
Old Post
moonhk
08-23-07 08:57 AM


Re: how to pass value to awk with space and ":" ?
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
08-23-07 12:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:59 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.