1830212 Members
1662 Online
109999 Solutions
New Discussion

Re: grep but not grep

 
SOLVED
Go to solution
Gemini_2
Regular Advisor

grep but not grep

Gurus

I have the following files

a.cpp
b.cpp
b.h
c.cpp
c.h
file1
file2
x.html
x1.html
x2.html
y.xml

is there a way that I can grep all the files that have *.cpp extension and all the files that dont have the html extensions


ls -1 | grep [*.cpp|!*.html]
I know the !.html will not work..how do I fix it?

thank you
9 REPLIES 9
Sandman!
Honored Contributor

Re: grep but not grep

Since you want only the .cpp files try the one below:

# ls -1 | grep ".cpp$"
A. Clay Stephenson
Acclaimed Contributor

Re: grep but not grep

Use the -v grep option as the not operator and you should anchor your search string so that you only look at suffixes.

ls -1 | grep -v -E '\.html$'
will list all files except those with .html suffixes. Note that the "\" is needed to escape the "." in regular expressions.

If it ain't broke, I can fix that.
Gemini_2
Regular Advisor

Re: grep but not grep

I apologize for not making myself clear..
I tried to give a simple example, but that is not what I wanted.

let me give a more closer to real life example

I have the following files

Makefile.defs@@/main/beta/0/Hijacked Makefile@@/main/beta/0
JBCINF.sbu
BCCOM.controller
alice33.cpp
file.cpp


I want to grap all the files except the files that end with @@/main/beta/0

please help out..
Gemini_2
Regular Advisor

Re: grep but not grep

these are two files

Makefile.defs@@/main/beta/0/Hijacked

Makefile@@/main/beta/0
James R. Ferguson
Acclaimed Contributor
Solution

Re: grep but not grep

Hi:

# grep -v \@\@/main/beta/0

Regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: grep but not grep

How about something like:

# ls -1 | grep -v "@@/main/beta/0"$

The $ will anchor the string to the end of the line.
Gemini_2
Regular Advisor

Re: grep but not grep

got it ! thank you!!!

Dennis Handly
Acclaimed Contributor

Re: grep but not grep

ls *.cpp would list all of the .cpp.

>I have the following files
Makefile.defs@@/main/beta/0/Hijacked

How can you get those files to show up in clearcase? ls -1 wouldn't ordinarily show multiple directory levels unless you used -R and it wouldn't descend into the @@ paths.

 

>I want to grab all the files except the files that end with @@/main/beta/0

 

You can use the shell Composite patterns:

ls -1 !(@@/main/beta/0)

Arturo Galbiati
Esteemed Contributor

Re: grep but not grep

Hi,
better to use:
grep -v \@\@/main/beta/0$ to be sure that teh search path be at the end.

HTH,
Art