filter grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting filter grep command
# 1  
Old 10-23-2009
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  
Old 10-23-2009
Quote:
Originally Posted by LRoberts
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  
Old 10-23-2009
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  
Old 10-23-2009
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 :-)
# 5  
Old 10-23-2009
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
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...
# 6  
Old 10-23-2009
try this:
Code:
/usr/ucb/ps -wwaxu | grep "[n]co_p_syslog .*$x" | awk '{print $2}'

# 7  
Old 10-23-2009
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
~>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter pattern in grep command

Hi, I am having a file like below hello how are you hello... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

2. Shell Programming and Scripting

Complex Filter using grep, awk or sed

Hi, I'm not very familiar witrh sed or awk and hope the somebody can help me to solve my problem. I need to filter a text report using grep, sed or awk. I would like to cut out text lines with the pattern INFO and if exists the following lines of the pattern DETAILS. I need te keep the lines with... (4 Replies)
Discussion started by: Frankg
4 Replies

3. Shell Programming and Scripting

Grep regex filter

Hi, How can I run the grep search in a script to only include compute, not bigcomputer in following search input? " properties = local compute" " properties = local bigcompute" Thank you. -j (6 Replies)
Discussion started by: hce
6 Replies

4. Shell Programming and Scripting

How set filter netstat -an | grep -P '\:'38''

Hi, I can write sh script for Linux platform I run: netstat -an | grep -P '\:'38''| grep ESTABLISHED but result: # netstat -an | grep -P '\:'38''| grep ESTABLISHED tcp 0 0 172.16.1.107:383 172.16.1.81:49981 ESTABLISHED tcp 0 0... (8 Replies)
Discussion started by: ostapv
8 Replies

5. Shell Programming and Scripting

How to filter only the last 'n' lines of a grep output?

I am running a grep query for searching a pattern, and the output is quite huge. I want only the last 200 lines to be displayed, and I am not sure if tail will do the trick (can tail read from std in/out instead of files?). Please help me out. (1 Reply)
Discussion started by: shell_newbie
1 Replies

6. Shell Programming and Scripting

Need to build a grep/sed/awk filter

Hi I need to to direct only the path and the name of the trace file to a new file. How do I use grep/awk/sed filter? eg. ABC.root>cat alert_omc_dg.log | grep trc ORA-00060: Deadlock detected. More info in file /u01/oradata/omc/udump/omc_dg_ora_3555.trc. ORA-00060: Deadlock detected. More... (8 Replies)
Discussion started by: geetap
8 Replies

7. Shell Programming and Scripting

Filter on a grep

I am attempting to figure out how to only capture part of a grep command I am doing. So far no luck. When I execute.... leviathan:/gfs/home/tivoli>ps -ef | /usr/ucb/ps -auxww | grep nco_p_syslog The results are.... tivoli 10185 0.0 0.0 5888 5168 ? S Oct 23 0:26... (2 Replies)
Discussion started by: LRoberts
2 Replies

8. UNIX for Advanced & Expert Users

ps avg | grep ? filter the desired out put.

Hi Folk, Following is the command I used to get data related to the DataFlowEngine. I wanted to know the % usage of cpu and memory. ps avg | grep Data This command will show the processes with its PID as : PID TTY STAT TIME PGIN SIZE RSS LIM TSIZ TRS %CPU %MEM COMMAND ... (1 Reply)
Discussion started by: varungupta
1 Replies

9. Shell Programming and Scripting

Grep script to filter

Hi, Wondering if anyone could help me with a simple script to filter out multiple things from a file. Right now I just have long lines of grep -v remove file | greg -v etc etc What I would like to do is have grep -v <run everything in a file> tofilter If that makes sense. Basically a... (2 Replies)
Discussion started by: kevin9
2 Replies

10. UNIX for Dummies Questions & Answers

Filter results through pipe with grep

ls -ltr | grep string How can I use regular expressions to filter the results provided even more. I am using the above command as a reference. (1 Reply)
Discussion started by: ckandreou
1 Replies
Login or Register to Ask a Question