Need help with searching and copying in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with searching and copying in a text file
# 1  
Old 04-26-2011
Need help with searching and copying in a text file

Hi,

I need help searching through a large text file. I need to find a certain string within the text, and copy each line until another string appears.


The file looks like this:
Code:
>scf15164843
ATTAAAGGNNNGGAATTTCCCCAA
ATTACCGGCTTTAAANNNTTACCC
>scf15154847
CCGGGNNNTTTAAACCCGNGNGCC
ATTANCGCGCGCATTAAATTTAAA
CTGTGTGAAAATCCGATAAGAGAG
GTGTGTGTGTTTGGTTTTTAAAAAA
>scf15164847
TTAGGTTGGAAAAAGGGTTTTAAG
GAAGGTTTTGGAAAAGTTAACCNN

And I need the output to be in another file as:
Code:
>scf15154847
CCGGGNNNTTTAAACCCGNGNGCC
ATTANCGCGCGCATTAAATTTAAA
CTGTGTGAAAATCCGATAAGAGAG
GTGTGTGTGTTTGGTTTTTAAAAAA

The part I need will always be between >scf######### and a > from another sequence.

Thanks in advance!
# 2  
Old 04-26-2011
Code:
nawk -v str='>scf15154847' '$0 ~ str {f=1;print;next} f &&!/^>/{print;next}f{f=0}' myFile


Last edited by vgersh99; 04-26-2011 at 06:50 PM..
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-26-2011
This will save the sections in separate files named out.<numberAfterscf>:
Code:
awk '$0{print ">scf"suff"\n" $0 > "out."suff}
       {suff=RT; sub(/>scf/,"",suff); sub(/\n/,"",suff)}
    ' RS='>scf[0-9]*\n*' ORS="" myFile


Last edited by mirni; 04-26-2011 at 09:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting and copying text from one file to another

Helooo, So I have a .fasta file (a text file with sequence data) which looks like this, with just over 3 million lines of data. >TCONS_00000001 gene=XLOC_000001 AATTGTGGTGAAATGACTTCTGTTAACGGAGACATCGATGATTGTTGTTACTATTTGTTCTCAGGATTCA... (8 Replies)
Discussion started by: 4galaxy7
8 Replies

2. Shell Programming and Scripting

Copying a file to multiple other files using a text file as input

Hello, I have a file called COMPLIST as follows that contains 4 digit numbers.0002 0003 0010 0013 0015 0016 0022 0023 0024 0025 0027 0030 0031 0032 0033 0035 0038 0041 (3 Replies)
Discussion started by: sph90457
3 Replies

3. Shell Programming and Scripting

Inserting text in file names while copying them.

I'm trying to find a Bourne shell script that will copy files from one directory using a wild card for the file name (*) and add some more characters in the middle of the file name as it is copied. As an example: /u01/tmp-file1.xml => /u02/tmp-file1-20130620.xml /u01/tmp-file2.xml => ... (6 Replies)
Discussion started by: Tony Keller
6 Replies

4. Shell Programming and Scripting

Need help searching through a text file.

I'm trying to get one specific number out of a text file, in order to use as part of an algorithm in a shell script. It will always come after a specific string and that string won't appear anywhere else in the file. So I'm trying to search through the file for that one string, then grab the... (2 Replies)
Discussion started by: mikedigornio
2 Replies

5. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

6. UNIX for Dummies Questions & Answers

Copying and Pasting columns from one text file to another

I have a tab delimited text file that I want to cut columns 3,4,5 from. Then I want to paste these columns into a space delimited text file between columns 2 and 3. I still want to keep the space delimited format in the final text file. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

7. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

8. UNIX for Dummies Questions & Answers

Searching directory for file that contains some text.

If I go into a directory and type in .. more * | grep foo I get the lines of text that contain foo in all of the files in the directory out of all of the files that are there. How do I make it so I can find out what the names of the files are that contain that text "foo"? By doing what I... (4 Replies)
Discussion started by: LordJezo
4 Replies

9. Shell Programming and Scripting

searching each file in a directory for text

what command can i use to search the files in a directory for a text. the output would list the files containing the text. ive tried this but it is not exactly what im looking to do: find . -name "*.xml" -exec agrep searchstring {} \; (2 Replies)
Discussion started by: jim majors
2 Replies

10. Shell Programming and Scripting

Help with searching a text file

Hello all! I've been working for days on this and it is really bugging me!! Here's my dilemma: Say I have a very large text file which contains fields delimited my a ':' which logs various records. Each record is separated by a newline character, therefore I can search for lines with... (6 Replies)
Discussion started by: thekid2
6 Replies
Login or Register to Ask a Question