LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   grep for multiple strings to exclude (https://www.linuxquestions.org/questions/linux-general-1/grep-for-multiple-strings-to-exclude-917138/)

greenpool 12-04-2011 08:52 PM

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:


2.txt

dump.2.new.1.txt
dump.2.new.3.txt
dump.2.new.4.txt

dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt

i tried the following which worked:

Code:

rm -rf $(ls * | grep -v  ^dump.2.new.*)
but ofcourse it delete 2.txt as well.

i've tried the following:

Code:

ls * | grep -v  "^dump.2.new.* | ^2.txt$"
ls * | grep -v  '^dump.2.new.* | ^2.txt$'

which doesn't work (it displays all the files) :(


Can somebody please tell me why this fails?

Dark_Helmet 12-04-2011 09:17 PM

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'
Also, be careful about your spaces. You might be adding them to make the expression more readable to you, but you may be inadvertently telling grep to match an unintended space.

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'

greenpool 12-04-2011 09:36 PM

Quote:

Originally Posted by Dark_Helmet (Post 4542134)
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'
Also, be careful about your spaces. You might be adding them to make the expression more readable to you, but you may be inadvertently telling grep to match an unintended space.

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'




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
dump.2.new.1.txt
dump.2.new.3.txt
dump.2.new.4.txt
dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt


my desired output is:
Code:

dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt


i tried it both ways but i can't seem to get it to work :confused:

Dark_Helmet 12-04-2011 10:02 PM

Here is a copy of my terminal run:
Code:

testuser@localhost ~$ ls -l
total 0
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 2.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 dump.2.new.1.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 dump.2.new.3.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.new.4.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.1.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.3.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.4.txt
testuser@localhost ~$ ls * | grep -v  '^dump.2.new.*\|^2.txt'
dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt
testuser@localhost ~$ ls * | grep -v  -e '^dump.2.new.*' -e '^2.txt'
dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt
testuser@localhost ~$ grep --version
GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

So, two things:

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).

raju.mopidevi 12-04-2011 11:11 PM

I tried this command and it worked for me
Code:

rm -rf $(ls -l | grep -v 'dump.2.new.*.txt')
Considering that file name has "dump.2.new." and "txt" will be constant and remaining will be variable.

raju.mopidevi 12-04-2011 11:30 PM

Quote:

Originally Posted by Dark_Helmet (Post 4542156)
Here is a copy of my terminal run:
Code:

testuser@localhost ~$ ls -l
total 0
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 2.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 dump.2.new.1.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 dump.2.new.3.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.new.4.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.1.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.3.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.4.txt
testuser@localhost ~$ ls * | grep -v  '^dump.2.new.*\|^2.txt'
dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt
testuser@localhost ~$ ls * | grep -v  -e '^dump.2.new.*' -e '^2.txt'
dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt
testuser@localhost ~$ grep --version
GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


Does this folder(where ever you have tried above command) contains any sub folders ?

greenpool 12-05-2011 01:19 AM

Quote:

Originally Posted by Dark_Helmet (Post 4542156)
Here is a copy of my terminal run:
Code:

testuser@localhost ~$ ls -l
total 0
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 2.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 dump.2.new.1.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:10 dump.2.new.3.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.new.4.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.1.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.3.txt
-rw-r--r-- 1 testuser testuser 0 Dec  4 21:11 dump.2.old.4.txt
testuser@localhost ~$ ls * | grep -v  '^dump.2.new.*\|^2.txt'
dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt
testuser@localhost ~$ ls * | grep -v  -e '^dump.2.new.*' -e '^2.txt'
dump.2.old.1.txt
dump.2.old.3.txt
dump.2.old.4.txt
testuser@localhost ~$ grep --version
GNU grep 2.6.3

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

So, two things:

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).

Thanks DH.

I think its the version of grep @ work thats incompatible.

I tried it at home and it works like a charm!

Dark_Helmet 12-05-2011 04:20 AM

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.