The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" DNAx86 Shell Programming and Scripting 13 04-17-2008 11:53 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
grep to find content in between curly braces, "{" and "}," keshav_rk Shell Programming and Scripting 4 08-09-2007 10:14 PM
grep/cat/more -- search in a txt file and display content from a specific "keyword" I-1 UNIX for Dummies Questions & Answers 4 02-21-2007 04:57 AM
Print The ouput From ls | grep "!!!" geoquest UNIX for Advanced & Expert Users 5 04-11-2002 05:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2008
hariza hariza is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 30
MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys,


I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys.


MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' `


Harby.
  #2 (permalink)  
Old 09-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Anything will match the empty string; regular expressions look for a match anywhere, and finding "nothing" anywhere trivially matches. You need to anchor it to make it more picky about what to find. $7 ~ /^$/ checks for an empty string (beginning of string adjacent to end of string) but it's probably more efficient to simply use equivalence comparison against the empty string.

Also, avoid the Useless Use of grep | grep | awk.

Code:
MEM=`ps v $PPID | awk 'tolower ($0) ~ /[d]b2/ { if ($7 == "") print 0; else print $7; }
(For additional succinctness, { print ($7 == "" ? 0 : $7) }.)

Last edited by era; 09-18-2008 at 02:06 AM.. Reason: Regex vs equivalence
  #3 (permalink)  
Old 09-18-2008
hariza hariza is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 30
hi ,

Thanks for your reply. For some reason still doesn't work for me. I took some parts of your command to at least get it to work using :

MEM=`ps v $PPID | grep -i db2| awk '{ if ($7 == "") print 0; else print $7; }`

and with the debug on I get the below:

MEM2=175804
+ + ps v 1790056
+ grep -i db2
+ awk { if ($7 == "") print 0; else print $7; }
MEM=
+ print

so you can see that the variable MEM suppose to be set to 0 but is not. Thanks again and thanks for your sugesstions. It take a while to come with a good coding techniques.
  #4 (permalink)  
Old 09-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Does the grep return any match?
  #5 (permalink)  
Old 09-18-2008
hariza hariza is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 30
Hi ,

Yes the grep is returning the PPID's and then I execute a ps v PPID as you can see below:

MEM=15244
+ + bc
+ echo 1567008 + 15244
MEM2=1582252
+ + ps v 1847476
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=16824
+ + bc
+ echo 1582252 + 16824
MEM2=1599076
+ + ps v 1896670
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=11072
+ + bc
+ echo 1599076 + 11072
MEM2=1610148
+ + ps v 1900608
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=13840
+ + bc
+ echo 1610148 + 13840
MEM2=1623988
+ + ps v 1912972
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=15404
+ + bc
+ echo 1623988 + 15404
MEM2=1639392
+ + ps v 1925356
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=16752
+ + bc
+ echo 1639392 + 16752
MEM2=1656144
+ + ps v 1974472
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=16012
+ + bc
+ echo 1656144 + 16012
MEM2=1672156
+ + ps v 1998882
+ grep -i db2
+ awk { if ($6 == "") print 0; else print $6; }
MEM=15852
+ + bc
+ echo 1672156 + 15852
MEM2=1688008
+ + bc
+ echo 1688008 / 1024
TOTAL=1648
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0