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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else
# 1  
Old 09-18-2008
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  
Old 09-18-2008
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 03:06 AM.. Reason: Regex vs equivalence
# 3  
Old 09-18-2008
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. Smilie
# 4  
Old 09-18-2008
Does the grep return any match?
# 5  
Old 09-18-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print the "grep" result as specified keyword order?

I have a content.xls file as given below, NC_020815.1 1891831 1894692 virb4_A0A0H2X8Z4_ 1 954 1945 NC_020815.1 1883937 1886123 vird4_A0A0P9KA26_ 1 729 1379 NC_020815.1 2976151 2974985 virb10_H8FLU5_Ba 1 393 478 NC_020815.1 2968797 2967745 virb6_A0A0Q5GCZ4 5 398 499... (2 Replies)
Discussion started by: dineshkumarsrk
2 Replies

2. Shell Programming and Scripting

Cmd="grep 'Name:' |awk -F' ' '{print $2}'"

Hi Is it possible to get the below code working.? cmd="grep 'Name:' |awk -F' ' '{print $2}'|xargs -i basename {}" echo $cmd ( rman target / <<EOF1 LIST COPY ; exit EOF1 ) | `$cmd` in nutshell I want to be able to preset cmd as depending on script flow it can be... (2 Replies)
Discussion started by: zam
2 Replies

3. Shell Programming and Scripting

Pick first "code" only [ grep/awk ]

All I have a requirement to search and pick the data as below. Explained with example. 38999|4812 Highway 52 North|Rockville|55901|0196 67541|2800 Dexter Road|Northville|38999|0196 This is pipe separate data. First column represents dealer id and 4th column represents the zip code where... (4 Replies)
Discussion started by: ak835
4 Replies

4. Shell Programming and Scripting

regex, awk, grep question "how to"

I've been working on this for 2 days and I'm not getting far. It is time to turn to you guys. With the data below, I am trying to create a file that looks like this: I'd like to use some form of egrep I think. AY#box#P04prod_to_contingency s AY#cmd#P04dump_cont_db s AY#cmd#P04get_on_ice_job s... (2 Replies)
Discussion started by: rawbi01
2 Replies

5. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

7. Shell Programming and Scripting

grep '\~' b | awk '{print $1","$3}' | sed -e 's/~//g'

Hi all, grep '\~' b | awk '{print $1","$3}' | sed -e 's/~//g' Iam using above command for some report... can this be done using any one of them either sed or awk or grep... (3 Replies)
Discussion started by: harshakusam
3 Replies

8. Shell Programming and Scripting

ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt"

Hi, I don't know hot to make this command work: ls -laR | grep "^-" | awk '{print $9}'| grep "$.txt" It should return the list of file .txt It's important to search .txt at the end of the line, becouse some file name have "txt" in their name but have other extensions (13 Replies)
Discussion started by: DNAx86
13 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Advanced & Expert Users

Print The ouput From ls | grep "!!!"

guys forget the find command coz with find command u can't get condational output like grep. I will give small example :- Apr 10 09:12 aacbl222_12aug1998.lqc Apr 10 09:12 sscbl4534_4sep2001.lqc Apr 10 09:12 ah66fmi_5jan1997.lqc Apr 10 09:12 y313h1_7sep1998.lqc May 11 09:12... (5 Replies)
Discussion started by: geoquest
5 Replies
Login or Register to Ask a Question

Featured Tech Videos