LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-21-2015, 04:59 AM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Question grep: find files that do not have multiple different strings


Hi all,

I'm trying to identify files that do not have matches for certain strings. FYI, these are files of DNA sequences and I'm trying to find those that are NOT sampled for any species by my group of interest (e.g., genes that are specific to that group of organisms).

I tried this code but it's actually yielding a list of files that DO match for my regexp.
Code:
for FILENAME in *.fas
do
grep -q -L ">PBAH" $FILENAME && grep -q -L ">SKOW" $FILENAME && grep -q -L ">CGRA" $FILENAME && echo $FILENAME
done
Basically I want to somehow go through and file files that do not contain ">PBAH" ">SKOW" or ">CGRA". Any assistance would be greatly appreciated!

Best,
Kevin
 
Old 04-21-2015, 05:10 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
awk would be better I think (or perl, python ...)
Code:
(pseudo code)
awk ' />PBAH/ { next file }
      />SKOW/ { next file }
      ....
      END { print filename }
    ' *.pas
If I understand it well
 
Old 04-21-2015, 05:21 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
first, using -q and and -L together seems a bit nonsensical. -L tells grep to print filenames that don't match, but -q supresses the output.
Also, you seem to be confusing standard output of the program and its return code. The && operators depend on the latter, while -L affects the former.
So, if the file does contain the first pattern, grep returns 0 and the next grep is executed. If, however, the first pattern is not included, grep prints nothing because of the -q switch and returns 1, which means the tests for the other patterns are not executed.
Also, you are running the greps in a loop, but the -L switch makes most sense when executed with multiple filenames to list those that don't match.

So, some possibilities:

Code:
grep -L "PBAH\|SKOW\|CGRA" *.fas
or, if you have more patterns, you can use the -f switch. so , if your patterns are in file patterns, one per line:

Code:
grep -L -f patterns *.fas
should work. There's plenty of variations, but I hope this will get you started.

Last edited by millgates; 04-21-2015 at 05:31 AM.
 
Old 04-22-2015, 08:23 AM   #4
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Thanks guys! Very helpful!
 
Old 04-22-2015, 01:55 PM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
glad to help you
(if you really want to say thanks just click on yes)
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep multiple strings GEEXTER Linux - General 7 12-06-2013 10:56 PM
[SOLVED] grep for multiple strings to exclude greenpool Linux - General 7 12-05-2011 05:20 AM
[SOLVED] Recursive search for strings in files with a certain date: find -name or grep -R? wolverene13 Linux - Newbie 6 10-01-2011 06:05 PM
[SOLVED] grep multiple strings krist_m Linux - Newbie 4 01-11-2011 12:43 PM
How to find multiple strings into multiple files ? moicpit Linux - General 10 04-25-2010 12:19 PM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration