Operating System - HP-UX
1830899 Members
2206 Online
110017 Solutions
New Discussion

how to grep a exactly string?

 
SOLVED
Go to solution
Stanley_8
Advisor

how to grep a exactly string?

hi~

If I grep "ABC",
I will receive the result like follows:

ABC
ABCD
ABCDEF

If I only want the result "ABC" return, how does it do?
16 REPLIES 16
Franky Leeuwerck_1
Regular Advisor

Re: how to grep a exactly string?

Hi,

You can use : grep "ABC "

Franky
Stefan Farrelly
Honored Contributor

Re: how to grep a exactly string?


grep "ABC$"

Im from Palmerston North, New Zealand, but somehow ended up in London...
Stanley_8
Advisor

Re: how to grep a exactly string?

No..
It's not working!

ex:
#ps -ef|grep "ABC"

It will return all the line contain "ABC".
I want it return include "ABC" exactly.
Not "ABCD" or "ABCDEFR".
Stanley_8
Advisor

Re: how to grep a exactly string?

Hi~

grep "ABC$" is not working too.

I can't find anything.
john korterman
Honored Contributor

Re: how to grep a exactly string?

Hi Stanley,
quite tricky. On 11.11 and onwards you can use the grep -w ABC for an exact match. On older versions you have to define something that makes it distinctive from the other possibilities, e.g.:
# grep ABC infile1 | grep -v "[D-Z]

but what can be used will depend on the situation.

regards,
John K.
it would be nice if you always got a second chance
Ernesto Cappello
Trusted Contributor

Re: how to grep a exactly string?

Hi Stanely


Stefan Farrelly
Honored Contributor

Re: how to grep a exactly string?

Very trickey. Im not sure even grep -w will do it for you. You must define more the rules under which you want to find ABC - at the start of the line ? end of line ? with blanks either side of it ? etc.

Try this;

ps -ef|awk '{print $9}'|grep -x ABC

But this will only work if the command part of ps has ABC only on it - nothing else, not /bin/ABC or ABCD etc.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Ernesto Cappello
Trusted Contributor

Re: how to grep a exactly string?

Hi Stanely

grep -w ABC

Regards.
Ernesto.
H.Merijn Brand (procura
Honored Contributor

Re: how to grep a exactly string?

l1:/tmp 103 > cat xx
ABC
AABC
ABCC
ABC
CBA
ABCD
ABCDE
ABC DEF
FABC D
l1:/tmp 104 > perl -ne '/\bABC\b/ and print' xx
ABC
ABC
ABC DEF
l1:/tmp 105 >


Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
V. V. Ravi Kumar_1
Respected Contributor

Re: how to grep a exactly string?

hi,

grep -x ABC

Regards
Never Say No
Umapathy S
Honored Contributor

Re: how to grep a exactly string?

hi Stanely,
As Ravi said, grep -x will do the trick.

from the man page
---
-x (eXact) Matches are recognized only when the
entire input line matches the fixed string or
regular expression.
---

Is this what you are looking for

HTH
Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Stanley_8
Advisor

Re: how to grep a exactly string?

my OS version is 11.00.
no "-w" function included.

I use "ps -ef" command and I want to grep the user name and the other information.
so I can't use the option "-x" to grep full line.
I try to use "awk" but only can find the user name . the other information was cut.
Ernesto Cappello
Trusted Contributor

Re: how to grep a exactly string?

Hi
I've this grep version
(grep -V)

grep (GNU grep) 2.2
(Cygnus)
Copyright (C) 1988, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

and if i've 2 file "psgrep" and "psgreps"

# >ll | grep -w psgrep
-rwx------ 1 X users 276 Dec 17 10:55 psgrep

# >ll | grep psgrep
-rwx------ 1 X users 276 Dec 17 10:55 psgrep
-rwx------ 1 X users 276 May 6 10:44 psgreps

Regards.
Ernesto.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: how to grep a exactly string?

# ps -fu203

gives you all the processes of user 203

# ps -ef

gives you *all* processes

"And other information" is much to vague for us to solve

Attached is a perl script that mimics ps behaviour (without using 'ps') and runs on HP-UX and AIX. Easy to modify to get other fields or info

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Jean-Louis Phelix
Honored Contributor

Re: how to grep a exactly string?

Hi,

Perhaps try it in another way ...

ps -fu login

Regards
It works for me (© Bill McNAMARA ...)
Umapathy S
Honored Contributor

Re: how to grep a exactly string?

hi Stanley,
The best alternate is to try ps -fu ABC instead of ps and grep.

HTH
Umapathy
Arise Awake and Stop NOT till the goal is Reached!