The UNIX and Linux Forums  

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
ps avg | grep ? filter the desired out put. varungupta UNIX for Advanced & Expert Users 1 04-10-2009 05:30 PM
Grep script to filter kevin9 Shell Programming and Scripting 2 02-21-2009 01:59 AM
how to filter `last` command for yesterday only skully Shell Programming and Scripting 1 06-26-2008 05:30 AM
grep a log file to filter previous dates pinpe Shell Programming and Scripting 5 08-03-2007 02:25 PM
Filter results through pipe with grep ckandreou UNIX for Dummies Questions & Answers 1 07-10-2006 03:04 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-23-2009
LRoberts LRoberts is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 87
filter grep command

I have ran into a small issue and I am not sure how to fix it.
In one of our current scripts we have this line which does a grep to get the pid of the process.


Code:
ps -ef | grep nco_p_syslog | grep $x | awk '{print $2}'

However this is not returning anything due to the how long the value of $x is. Which a sample value is...


Code:
ot1p_stdby

However as you can see a simple grep cuts this off...

Code:
tivoli 20343     1   0 10:16:09 pts/1       0:00 /lcl/apps/Tivoli/netcool/omnibus/probes/solaris2/nco_p_syslog -manager ot1p_std

I need to find a way to do a grep command like below only have it know off the _stdby contained within $x.
So that even though $x contains the value ot1p_stdby it should cutt off the _stdby and only by ot1p when it does the following command.,,,


Code:
ps -ef | grep nco_p_syslog | grep $x | awk '{print $2}'

I was not sure if there is a way to do this right in the command above.

Thanks
  #2 (permalink)  
Old 10-23-2009
dr.house dr.house is offline
Registered User
  
 

Join Date: Dec 2008
Location: Switzerland
Posts: 229
Quote:
Originally Posted by LRoberts View Post

Code:
ps -ef | grep nco_p_syslog | grep $x | awk '{print $2}'
Ad hoc (untested):


Code:
ps -ef | grep nco_p_syslog | grep "${x:0:8}" | awk '{print $2}'

  #3 (permalink)  
Old 10-23-2009
LRoberts LRoberts is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 87
Gave it a try but got this...

Code:
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>./test.sh
./test.sh[3]: "${x:0:8}": bad substitution
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>

Here is how I am testing...


Code:
#!/bin/ksh
x=entp_stdby
ps -ef | grep nco_p_syslog | grep "${x:0:8}" | awk '{print $2}'


Changed the " to ' and that fixed the error...

Code:
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>./test.sh
./test.sh[3]: '${x:0:8}': bad substitution
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>

But still no results. Seems like that would have worked. I see where you was going with it. It should have take the $x which has a value of entp_stdby and only did a grep for entp_std but it returned no results.

As you can see it should have returned results as this is running out there.

Code:
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>ps -ef | grep syslog | grep  entp_std
  tivoli 20337     1   0 10:16:09 pts/1       0:01 /lcl/apps/Tivoli/netcool/omnibus/probes/solaris2/nco_p_syslog -manager entp_std

  #4 (permalink)  
Old 10-24-2009
dr.house dr.house is offline
Registered User
  
 

Join Date: Dec 2008
Location: Switzerland
Posts: 229
Quote:
Originally Posted by LRoberts View Post
Code:
"${x:0:8}": bad substitution
That's interesting, as on my workstation (Debian Linux 5 feat. Bash), this works ...


Code:
[house@leonov] a='nco_p_syslog -manager ot1p_std'; x='ot1p_stdby'; echo $a | grep "${x:0:8}"
nco_p_syslog -manager ot1p_std

Anyway, how about skipping single as well as double quotes entirely, as follows ...


Code:
[house@leonov] a='nco_p_syslog -manager ot1p_std'; x='ot1p_stdby'; echo $a | grep ${x:0:8}
nco_p_syslog -manager ot1p_std

  #5 (permalink)  
Old 10-23-2009
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
it's likely not the grep; it's probably the initial ps.

Assuming you're on solaris:

Code:
  /usr/ucb/ps -wwaxu | grep "nco_p_syslog .*$x" | awk '{print $2}'

should work...

Last edited by Tytalus; 10-23-2009 at 12:27 PM.. Reason: was not intending to be so blunt :-)
  #6 (permalink)  
Old 10-23-2009
LRoberts LRoberts is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 87
Yep now that works. It also shows the pid for the grep itself though as well. I am trying to figure out how this is working, lol. Lost me on this one.

How can I make it so it does not return 2 pids and only the one I am looking for.
Right now its returning the pid for this grep as well.

Code:
leviathan:/lcl/apps/Tivoli/netcool/omnibus/bin>./test.sh
10572
20337



Quote:
Originally Posted by Tytalus View Post
it's likely not the grep; it's probably the initial ps.

Assuming you're on solaris:

Code:
  /usr/ucb/ps -wwaxu | grep "nco_p_syslog .*$x" | awk '{print $2}'

should work...
  #7 (permalink)  
Old 10-23-2009
trey85stang trey85stang is offline
Registered User
  
 

Join Date: May 2008
Posts: 74
bash and i beilve ksh (the later one) support this type of substitution:


Code:
~> var1=nco_p_syslog
~> echo $var1
nco_p_syslog
~> echo ${var1/_syslog/}
nco_p
~> echo ${var1/_syslog/_newword}
nco_p_newword
~>

Reply

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 08:12 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