LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-04-2011, 08:52 PM   #1
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Rep: Reputation: 0
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?
 
Old 12-04-2011, 09:17 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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'

Last edited by Dark_Helmet; 12-04-2011 at 09:21 PM. Reason: Added clarity - and later for alternative
 
Old 12-04-2011, 09:36 PM   #3
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Dark_Helmet View Post
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
 
Old 12-04-2011, 10:02 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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).

Last edited by Dark_Helmet; 12-04-2011 at 10:11 PM.
 
1 members found this post helpful.
Old 12-04-2011, 11:11 PM   #5
raju.mopidevi
Senior Member
 
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Blog Entries: 12

Rep: Reputation: 92
Talking

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.
 
Old 12-04-2011, 11:30 PM   #6
raju.mopidevi
Senior Member
 
Registered: Jan 2009
Location: vijayawada, India
Distribution: openSUSE 11.2, Ubuntu 9.0.4
Posts: 1,155
Blog Entries: 12

Rep: Reputation: 92
Quote:
Originally Posted by Dark_Helmet View Post
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 ?

Last edited by raju.mopidevi; 12-05-2011 at 09:38 PM. Reason: for a better clarity
 
Old 12-05-2011, 01:19 AM   #7
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Dark_Helmet View Post
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!
 
Old 12-05-2011, 04:20 AM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep multiple strings GEEXTER Linux - General 7 12-06-2013 09:56 PM
[SOLVED] Can grep exclude symlinks ?? JamesEarlOwens Linux - Newbie 10 10-21-2011 04:05 AM
[SOLVED] grep multiple strings krist_m Linux - Newbie 4 01-11-2011 11:43 AM
[SOLVED] grep exclude Manuel-H Linux - General 4 10-12-2010 08:50 PM
script to exclude strings... NiallC Programming 10 03-28-2005 06:46 PM


All times are GMT -5. The time now is 04:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration