1833758 Members
2717 Online
110063 Solutions
New Discussion

Re: grep help

 
Hanry Zhou
Super Advisor

grep help

grep -v -E 'abc|xyz' fielname

This line will also exclude any name with strings of xyz, for instance, will exclude "defxyz". However, I want to exclude exactly "xyz" only, not "defxyz", Can I do that?

none
29 REPLIES 29
Pete Randall
Outstanding Contributor

Re: grep help

Why not just "grep -v 'abc' |grep -v 'xyz'"?


Pete

Pete
Karthik S S
Honored Contributor

Re: grep help

grep -v -E 'abc|^xyz' fielname

Will solve your problem.

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Michael Schulte zur Sur
Honored Contributor

Re: grep help

Hi,

is xyz, which you want to exclude the only item in the line? If so use:
grep -v -E 'abc|^xyz$' filename
otherwise try:
grep -v 'abc' filename | grep [a-z,A-Z]xyz[a-z,A-Z]

greetings,

Michael
Michael Schulte zur Sur
Honored Contributor

Re: grep help

Hi Pete,

if I am not mistaken then
grep -v -E 'abc|xyz' fielname
and
grep -v 'abc' fielname|grep -v 'xyz'
is the same
not (a or b) <=> not a and not b

or have I missed something?

Michael
Karthik S S
Honored Contributor

Re: grep help

Sorry about the wrong inputs. It should be

grep -E 'abc|\' filename

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: grep help

grep -v -E 'abc|\' filename

Forgot "-v" in the above posting. No points for this ;-)

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Klaas D. Eenkhoorn
Regular Advisor

Re: grep help

Let's try my first answer and to score my first points . . . ;-)

Have you tried:

grep -v -E 'abc|^xyz$' filename

The ^ means: starts with xyz
and $ means : ends with xyz

So ^xyz$ means : begins and also ends with xyz witch is in common language: it must be exact xyz.

Greetzzz,

Kl@@s
Karthik S S
Honored Contributor

Re: grep help

Hi Kl@@s,

I thought in similar lines too. Then I realized what he needs is to exclude a exact word whether it is in the start or mid or end of a line. So the best bet is \\ .... Better luck next time ;-)

I hope Hanry will be kind enough to encourage our new meber by assigning some points ....

-Karthik S S
No points for this Please ...
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
john korterman
Honored Contributor

Re: grep help

Hi,
I think it is almost impossible, as you have to define the surroundings for "xyz" that need to be exluded, e.g. spaces around the string, the string alone on a line, string at beginning of a line followed by space, a digit in front of the string etc.
An idiotic example of some of these:

# grep -v -E 'abc|[[:space:]]xyz[[:space:]]|^xyz$|^xyz[[:space:]]|[[:digit:]]xyz$|xyz[[:digit:]]' ./infile

where infile holds these lines:

abc
xyz
xyz def
xyz 123
xyz123
123xyz
defxyz

What you probably do not want is to search positively for a string, but I think that would be easier.

regards,
John K.
it would be nice if you always got a second chance
john korterman
Honored Contributor

Re: grep help

sorry, forgot the special forum formatting. These three lines of my former post:
xyz def
xyz 123
xyz123
should all start with a space.

it would be nice if you always got a second chance
Michael Schulte zur Sur
Honored Contributor

Re: grep help

Hi Hanry,

please define what exactly it is, you do not want to be excluded!

greetings,

Michael
Hanry Zhou
Super Advisor

Re: grep help

I want to exclude only exactly "abc", "xyz", ...

The line I have now grep -v -E 'abc|xyz' filename, will exclude all lines with strings including "abc", "xyz", ex, it will exclude "ertabc", "afxyzsdf", that is not what I want. I want to exlude lines including "abc", or "xyz", or...
none
Karthik S S
Honored Contributor

Re: grep help

Did you try this??

grep -v -E '\|\' filename

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Hanry Zhou
Super Advisor

Re: grep help

S S

THE LINE YOU SUGGESTED WOULD ALSO EXCLUDE "xyz" itself.
none
Hanry Zhou
Super Advisor

Re: grep help

The only solutions for my case seems to be:

grep -v abc | grep -v xyz | grep -v ...

It seems not very intelegency, but only one working.

'^xyz' not working is because the string is not located in the begining of the line, but in a field.
none
Karthik S S
Honored Contributor

Re: grep help

Hanry,

As per your first post you wanted to exclude ONLY xyz and your later statement conflicts that ???

-Karthik S S

Hanry:
This line will also exclude any name with strings of xyz, for instance, will exclude "defxyz". However, I want to exclude exactly "xyz" only, not "defxyz", Can I do that?
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Hanry Zhou
Super Advisor

Re: grep help

sorry for confusing. Let me try again

I have a file with follwoing format:

...

f1|f2|abc |f4|f5...|
f11|f22|frontxyz |f44|f55...|
f111|f222|xyzend |f444|f555..|
..
f1111|f2222|frontxyzend |f4444|f55555..|
...
f11111|f2222|..... | ....

I want to list all fields contain ONLY "abc", "xyz", "this"...a certain string?
none
Hanry Zhou
Super Advisor

Re: grep help

correction:

I want to EXCLUDE those with only "abc", "xyz", "this"... a few certain strings.
none
Dave La Mar
Honored Contributor

Re: grep help

I have assigned points to 167 of 394 responses to my questions.

Hanry -
Do I understand you to want the line but exclude a field within that line that contains the strings you mention?

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
Karthik S S
Honored Contributor

Re: grep help

If you wanted the way your file as Dave has explained then try this,

sed s/xyz//g filename | sed s/abc//g | sed s/somestring//g

-Karthik S S

Hanry, did you read the first line of the Dave's post??
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Michael Schulte zur Sur
Honored Contributor

Re: grep help

Hi,

attached is an awk script.
call awk -F"\|" awkscript yourfile

thats hopefully all you need,

Michael
Karthikeyan_5
Frequent Advisor

Re: grep help

Hi All,

I also got the similar requirement......i.e. I want to avoid all the lines containing the word "mathi" & not "mathivanan" or "abcmathitest" from my i/p file.....my sample i/p files looks as follows:

-------------------------------
1 mathi a bcdtest
2xyz mathivanan hopeso
3 hanry
4karthik s s mathi
5 Michael Schulte mathiabc
6 Pete Randall testmathi
7Klaas D. Eenkhoorn mathi
8john korterman aa mathi
9 Dave La Mar smathia
-------------------------------

I want the ouput after avoiding the lines containing exactly the word "mathi".....which'll be as follows:

-------------------------------
2xyz mathivanan hopeso
3 hanry
5 Michael Schulte mathiabc
6 Pete Randall testmathi
7Klaas D. Eenkhoorn mathi
9 Dave La Mar smathia
-------------------------------

I hope I made my requirement very clear......I want to know how i can get this by way of "grep" or any script which'll do this job for me......

Thanks in advance.....

Regards,
karthik
Michael Schulte zur Sur
Honored Contributor

Re: grep help

Hi Karthik,

would you please open a new thread for this? You get better attention and are able to award points for the help given.

Michael
Karthikeyan_5
Frequent Advisor

Re: grep help

Hi Michael,

I've posted as a new topic.....thanks for correcting my fault....

regards,
karthik