LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 03:59 AM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Tuscaloosa, AL
Posts: 126

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, 04:10 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
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, 04:21 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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 04:31 AM.
 
Old 04-22-2015, 07:23 AM   #4
kmkocot
Member
 
Registered: Dec 2007
Location: Tuscaloosa, AL
Posts: 126

Original Poster
Rep: Reputation: 15
Thanks guys! Very helpful!
 
Old 04-22-2015, 12:55 PM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

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


Reply



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 09:56 PM
[SOLVED] grep for multiple strings to exclude greenpool Linux - General 7 12-05-2011 04: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 05:05 PM
[SOLVED] grep multiple strings krist_m Linux - Newbie 4 01-11-2011 11:43 AM
How to find multiple strings into multiple files ? moicpit Linux - General 10 04-25-2010 11:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:52 AM.

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
Open Source Consulting | Domain Registration