grep for multiple strings to exclude
Hi,
I'm looking to delete files in a folder. because there many different file names, i thought its best to remove files that do not match the grep statement. folder looks like this: Code:
Code:
rm -rf $(ls * | grep -v ^dump.2.new.*) i've tried the following: Code:
ls * | grep -v "^dump.2.new.* | ^2.txt$" Can somebody please tell me why this fails? |
You have to escape the '|' in your expression for grep to interpret it as a logical OR rather than a literal '|'
Try: Code:
ls * | grep -v '^dump.2.new.*\|^2.txt' EDIT: You can also rewrite the command to separate your matches. For instance, the following command gave me identical results: Code:
ls * | grep -v -e '^dump.2.new.*' -e '^2.txt' |
Quote:
Hi, Thanks for your reply. I tried your suggestion and it doesn't seem to quite work for me. everytime i run it i get: Code:
2.txt my desired output is: Code:
dump.2.old.1.txt i tried it both ways but i can't seem to get it to work :confused: |
Here is a copy of my terminal run:
Code:
testuser@localhost ~$ ls -l 1. Are you copying the commands exactly as-is AND running them from a terminal exactly as-is? 2. If so, compare your grep version to mine. If yours is different, then that might explain the problem. EDIT: Your OS icon next to your posts shows you are running Windows. If you are using Cygwin, I don't know if it includes a port of grep that supports all the features found in a native linux install. I ran the above commands on a Debian system. I have no experience using Cygwin and do not know what limitations it has (if any). |
I tried this command and it worked for me
Code:
rm -rf $(ls -l | grep -v 'dump.2.new.*.txt') |
Quote:
|
Quote:
I think its the version of grep @ work thats incompatible. I tried it at home and it works like a charm! |
Glad it works... at least half the time.
At an old job of mine, we VNC'd into a Linux environment to do work. That environment didn't have all the tools and/or versions I was used to working with. The admins did give us a decent quota and gave us access to gcc. So, if the same is true for your setup, you might do the same thing I did: download the source for the tool(s), compile, and install them locally to your home directory. You can work your way to an appropriate tarball from here: GNU Grep Page |
All times are GMT -5. The time now is 11:23 PM. |