LinuxQuestions.org
Review your favorite Linux distribution.
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 02-18-2013, 11:29 AM   #1
HeidiJTP
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Rep: Reputation: Disabled
Why is grep output file the same as original search file?


I'm trying to extract the contents of one file (infile.txt) from another file (mainfile.fastq). I'm using the command:

$ grep -f infile.txt mainfile.fastq

In the terminal, the complete contents of mainfile.fastq are shown, with the matching lines from infile.txt highlighted.
If I try to output matching lines, I get the complete contents of mainfile.fastq:

$ grep -f infile.txt mainfile.fastq >out.txt

I'm trying to get this to work so I can use a modified command on a larger file along with -A to extract trailing context as well. The output will be too much to read in the terminal.

Any suggestions?
 
Old 02-18-2013, 11:37 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
So what does "infile.txt" exactly contain? I mean how "greedy" (or exact) are your matches?
 
Old 02-18-2013, 11:39 AM   #3
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: Debian 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,766

Rep: Reputation: Disabled
diff is better suited for this scenario.
 
Old 02-18-2013, 12:29 PM   #4
HeidiJTP
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
unSpawn:

Here are the contents of my infile.txt:
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/70114/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/6850/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/22749/ccs

In the terminal window, I get the full mainfile.txt with matching lines highlighted:
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/49/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/2229/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/22449/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/149/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/2802/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/20/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/749/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/70114/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/6850/ccs

@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/11523/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/81245/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/27245/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/1634/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/959/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/15423/ccs
@m130129_192343_42154_c100435262550000001523067002011393_s1_p0/834/ccs

The outfile just duplicates the mainfile.txt
 
Old 02-18-2013, 12:32 PM   #5
HeidiJTP
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by amani View Post
diff is better suited for this scenario.
Amani - Would that allow me to output lines that are the same between files? And is there an option for including trailing information, similar to grep -A? My end goal is not just to extract lines that are the same, but to use the infile to extract additional information from a mainfile.
 
Old 02-18-2013, 12:46 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Does infile.txt contain blank lines (maybe at the end)? A blank line would match anything.
 
Old 02-18-2013, 01:37 PM   #7
HeidiJTP
LQ Newbie
 
Registered: Feb 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Does infile.txt contain blank lines (maybe at the end)? A blank line would match anything.
Yup, good call! Works perfectly now, even when modified with -A to pull out additional lines from a larger file. Thanks so much!
 
Old 02-19-2013, 11:54 AM   #8
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: Debian 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,766

Rep: Reputation: Disabled
Quote:
Originally Posted by HeidiJTP View Post
Amani - Would that allow me to output lines that are the same between files? And is there an option for including trailing information, similar to grep -A? My end goal is not just to extract lines that are the same, but to use the infile to extract additional information from a mainfile.
Is it not possible with the
--LTYPE/GTYPE-line-format=... options?
 
  


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
[SOLVED] adding diff output to the original file elliotd123 Linux - General 6 11-07-2012 08:44 AM
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 2 04-17-2012 02:22 PM
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 1 04-15-2012 04:07 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

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

All times are GMT -5. The time now is 02:57 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