Operating System - HP-UX
1827811 Members
2032 Online
109969 Solutions
New Discussion

grep the string only, not including grep command itself

 
Hanry Zhou
Super Advisor

grep the string only, not including grep command itself

When I "grep" a string there is always a additional line for the grep command itself come out. HOw do I have grep only come out one line, in stead of two line?

Ex, ps -ef | grep vhand
two line output:
............vhand
............grep vhand
none
7 REPLIES 7
Patrick Wallek
Honored Contributor

Re: grep the string only, not including grep command itself

Add a '| grep -v grep' to your command.

# ps -ef | grep vhand | grep -v grep
James R. Ferguson
Acclaimed Contributor

Re: grep the string only, not including grep command itself

Hi:

# ps -ef|grep vhand|grep -v "grep vhand"

...is your direct answer

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: grep the string only, not including grep command itself

Hi (again):

...however, if you are looking for a process(es) by name, a better way is to leverage the XPG4 (UNIX95) option to select the process by its basename and display the data you want. FOr instance:

# UNIX95= ps -C vhand -o args,pid,ppid,sz,comm

To eliminate the headings from 'ps':

# UNIX95= ps -C vhand -o args,pid,ppid,sz,comm|awk '/NR>1/'

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: grep the string only, not including grep command itself

I have a script called psg which does this automatically, without having to type out the command line options.

I am attaching it. Usage is psg it does reliably eliminate the grep process.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Paul Murray_4
Frequent Advisor

Re: grep the string only, not including grep command itself

Hi,

Another alternative to the "| grep -v grep" extension is to enclose the first character of your grep string inside [] - basically, this turns the command into a regular expression.

For example:-

$ ps -ef | grep vhand | grep -v grep

can also be written as:-

$ ps -ef | grep [v]hand


This is a quick and easy alternative when entering the command interactively, but the "| grep -v grep" is far easier to use within shell scripts, especially if the grep string is a substituted variable.


Rgds,
Paul.
Hey, nobody knows EVERYthing !!!
Caesar_3
Esteemed Contributor

Re: grep the string only, not including grep command itself

Hello!

Do:
ps -ef |grep -v grep | grep vhand

Caesar
Kevin Wright
Honored Contributor

Re: grep the string only, not including grep command itself

ps -ef |grep [v]hand