LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-31-2012, 08:54 PM   #16
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845

um, you need to read the man pages.

what you are trying to do above is search for the string "MSENGNKNIAIVEAFSETDKKTGEVVTLVPNTNNTVQPVALMRLGLFVPTLKSTSRGRKGQMVSMDASAELKQLSLAKAEGYEDIRISGLRLDMDNDFKT WVGIIHAFAKHKVVGDTVTLPFVEFVRLCGIPTARSSAKLRKRLDSSLSRIATNTISFRSKGSDEFYVTHLVQTAKYSVKHDTVELKADPKIFELYQFDK KVLLQLRAINELSRKESAQALYTFIESLPPDPAPISLARLRARLNLTSRTITQNATVRKAMEQLREIGYLDYTEVKRGNSVYFVIHYRRPKLRQAQISTK IDNDETEYSLPDENQDDIIDVVPDEKEGKMVMLSKEELALLEELRKAKTRK" in file_1.txt
 
1 members found this post helpful.
Old 11-01-2012, 04:07 AM   #17
Adzrules
LQ Newbie
 
Registered: Oct 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
um, you need to read the man pages.

what you are trying to do above is search for the string "MSENGNKNIAIVEAFSETDKKTGEVVTLVPNTNNTVQPVALMRLGLFVPTLKSTSRGRKGQMVSMDASAELKQLSLAKAEGYEDIRISGLRLDMDNDFKT WVGIIHAFAKHKVVGDTVTLPFVEFVRLCGIPTARSSAKLRKRLDSSLSRIATNTISFRSKGSDEFYVTHLVQTAKYSVKHDTVELKADPKIFELYQFDK KVLLQLRAINELSRKESAQALYTFIESLPPDPAPISLARLRARLNLTSRTITQNATVRKAMEQLREIGYLDYTEVKRGNSVYFVIHYRRPKLRQAQISTK IDNDETEYSLPDENQDDIIDVVPDEKEGKMVMLSKEELALLEELRKAKTRK" in file_1.txt
Indirectly though. I want to find the corresponding CDS.001 line in file_1.txt and then pull the next few lines below that record so that I can obtain the MSENGNKNIAIVEAFSETDKKTGEVVTLVPNTNNTVQPVALMRLGLFVPTLKSTSRGRKGQMVSMDASAELKQLSLAKAEGYEDIRISGLRLDMDNDFKT WVGIIHAFAKHKVVGDTVTLPFVEFVRLCGIPTARSSAKLRKRLDSSLSRIATNTISFRSKGSDEFYVTHLVQTAKYSVKHDTVELKADPKIFELYQFDK KVLLQLRAINELSRKESAQALYTFIESLPPDPAPISLARLRARLNLTSRTITQNATVRKAMEQLREIGYLDYTEVKRGNSVYFVIHYRRPKLRQAQISTK IDNDETEYSLPDENQDDIIDVVPDEKEGKMVMLSKEELALLEELRKAKTRK bits.

I suppose I effectively want to say: "Find this record from file_1.txt in file_2.txt and then copy that line and the next few lines until a ">CDS.*" is encountered."
 
Old 11-01-2012, 04:21 AM   #18
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
What schneidz wanted to say is that you mixed up the files. You search patterns from file_2 in file_1 instead the other way around. The filename after -f should be the one with what you want to search, while the other one is the one in which you want to search.

Last edited by millgates; 11-01-2012 at 04:25 AM.
 
Old 11-01-2012, 05:37 AM   #19
Adzrules
LQ Newbie
 
Registered: Oct 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
What schneidz wanted to say is that you mixed up the files. You search patterns from file_2 in file_1 instead the other way around. The filename after -f should be the one with what you want to search, while the other one is the one in which you want to search.
I see.

However, if I switch things around, where I'm searching for fgrep -f FileIWantToSearch FileWithinWhichToSearch, I get a blank text file again. I've tried it both ways round and always get a blank text file.
 
Old 11-01-2012, 06:02 AM   #20
Adzrules
LQ Newbie
 
Registered: Oct 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Okay guys, thanks ever so much for your help! It has been much appreciated. My apologies for my idiocy at points, but I have solved it myself now with the following command:

Code:
for i in `cat file_1.txt`; do grep $i file_2.txt -A 1; done
 
Old 11-01-2012, 07:18 AM   #21
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Hm, it seems that there are trailing lines in your file_1.txt. That's why the grep -f did not work. It assumed the spaces are part of the pattern to search. If you remove the spaces, it should work.

Edit: I meant "trailing spaces", of course, not "trailing lines"

Last edited by millgates; 11-01-2012 at 08:55 AM.
 
1 members found this post helpful.
Old 11-03-2012, 12:53 PM   #22
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
This is also incorrect in several ways.
Code:
for i in `cat file_1.txt`; do grep $i file_2.txt -A 1; done
1) Don't Read Lines With For. Use a while+read loop.

2) Useless Use Of Cat. Although in this case it's really part and parcel of #1.

3) QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

4) In addition, while not wrong, per se, $(..) is highly recommended over `..`. Backticks are deprecated syntax.

Code:
while IFS='' read -r entry ; do
	grep -A 1 "$entry" file_2.txt
done <file_1.txt
I added IFS='' to preserve leading and trailing whitespace in the file. Leave this off if this isn't an issue, or if you want it removed.

grep -f is probably still the best option, however, although you need to be sure the matching patterns are exact, as millgates mentioned. Another thing to watch out for is that your files shouldn't have dos-style line endings. The extra carriage return would generally make matches fail.
 
1 members found this post helpful.
  


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
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 11:52 AM
[SOLVED] Find Text After String Search in Text File redir Linux - Newbie 12 08-02-2011 04:57 PM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 10:40 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
using DD to pull records out of a data file fhinkle Linux - Newbie 6 02-24-2005 05:20 PM


All times are GMT -5. The time now is 06:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration