Need help separating a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help separating a file
# 1  
Old 07-22-2011
Need help separating a file

Hi all,

I have a single text file, Contig3.fasta, that looks like this:

Code:
>NAME1
ACCTGGTA
>NAME2
GGTTGGACA
>NAME3
ATTTTGGGCC

And It has about 100 items like this in it. What I would like to do is copy each item into 100 different text files, and have them named a certain way

Code:
Output should be 3 different files in this example:
File 1 will be named AS001.C21_A01 and contain:
>NAME1
ACCTGGTA

File2 will be named AS001.C21_A02 and contain:
>NAME2
GGTTGGACA

File3 will be named AS001.C21_A03 and contain:
>NAME3
ATTTTGGGCC

So essentially, I need to copy from one ">" until the next one and output that into a file named AS001.C21_AXX and to keep doing it until it reaches the end.
# 2  
Old 07-22-2011
Code:
awk 'END {
  print r > ( f sprintf( "%02d", ++c ) )
  }
/^>/ && r {
  print r > ( f sprintf( "%02d", ++c ) ) 
  r = x; close( f sprintf( "%02d", c ) )
  }
{ 
  r = r ? r RS $0 : $0 
  }' f="AS001.C21_A" Contig3.fasta

This User Gave Thanks to radoulov For This Post:
# 3  
Old 07-22-2011
Amazing, thanks so much radoulov!
# 4  
Old 07-22-2011
Code:
csplit -z -f AS001.C21_A Contig3.fasta '/>NAME/' '{*}'

This User Gave Thanks to binlib For This Post:
# 5  
Old 07-23-2011
Quote:
Originally Posted by binlib
Code:
csplit -z -f AS001.C21_A Contig3.fasta '/>NAME/' '{*}'

Yes,
that would be the right tool for such a task.
The only possible drawback, if I recall correctly,
is that not all implementations support unlimited number of
repetitions (the numbering begins with 0, but that
could be easily worked around by putting a dummy entry
at the beginning of the input file).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in separating a multilingual file

Hello, I have a text file running into around 100 thousand+ lines which has the following rigid structure: Each field is separated by a comma. Some examples are given below: 23,Chinttaman Pagare,चिंतमण पगारे 24, Chinttaman Pateel,चिंतामण पाटल 25, Chinttaman Rout,चिंतामण राऊत 26,... (3 Replies)
Discussion started by: gimley
3 Replies

2. Shell Programming and Scripting

Need help separating file lines into three classes

Hi folks, What I have are config files with lines that: are blank, start with a "!" or start with char's(or a blank space and then char's) I am using ksh I can display each line by doing: for INDEX in {0..$LENGTH} do echo "${data}" done What I need to do requires I can... (12 Replies)
Discussion started by: Marc G
12 Replies

3. Shell Programming and Scripting

How to generate a csv files by separating the values from the input file based on position?

Hi All, I need help for doing the following. I have a input file like: aaaaaaaaaabbbbbbbbbbbbbbbbbbbb cccbbbbbaaaaaadddddaaaabbbbbbb now I am trying to generate a output csv file where i will have for e.g. 0-3 chars of each line as the first column in the csv, 4-10 chars of the line as... (3 Replies)
Discussion started by: babom
3 Replies

4. Shell Programming and Scripting

AWK separating a file into an array

Is there a way to have awk put successive records into an array in a bash script? I have files that say things like name :title :salary Bob :Instructor :30,000 Joyce :Instructor :30,000 Patrick :Manager :40,000 What I want to do is seperate this file into an array so that... (8 Replies)
Discussion started by: tgidzak
8 Replies

5. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

6. Shell Programming and Scripting

Separating fields

Hi, I have a text file in following format: 2.45 5.67 6.43 I have to cut the values before decimal and store them in a file. So the output file should look like: 2 5 6 . . and so on... Can someone suggest me a sed/awk command for doing this? (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

7. Shell Programming and Scripting

Separating delimited file by pattern with exclusion list

I have a file with the contents below jan_t=jan;feb_t=feb;mar_t=mar;year=2010 jan_t=null;feb_t=feb;mar_t=mar;year=2010 jan_t=jan;feb_t=feb;mar_t=mar;year=2010 I want to extract out all the fields values ending with "_t" , however, i want to exclude feb_t and mar_t from the results In... (6 Replies)
Discussion started by: alienated
6 Replies

8. Shell Programming and Scripting

Merging files into a single tab delimited file with a space separating

I have a folder that contains say 50 files in a sequential order: cdf_1.txt cdf_2.txt cdf_3.txt cdf_3.txt . . . cdf_50.txt. I need to merge these files in the same order into a single tab delimited file. I used the following shell script: for x in {1..50}; do cat cdf_${x}.txt >>... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

9. Shell Programming and Scripting

Separating values from a file and putting to a variable

I am writing into a file testfile.txt values like ./XXXXXXCZ1/tprcm10c.bin ./XXXXXXCZ1_HOT/tprcm09c.bin ./XXXXXXCZ_cold/tprcm05c.bin I want to store the values of tprcm*.bin and XXXXXXCZ* in separate variables Can anybody Pls hlp me out with this ... Thanks (2 Replies)
Discussion started by: ultimatix
2 Replies

10. Shell Programming and Scripting

separating fields

Hi, i have a file as follows: jonathan:bonus1,bonus2 gerald:bonus1 patrick:bonus1,bonus2 My desired output is jonathan:bonus1 jonathan:bonus2 gerald:bonus1 patrick:bonus1 patrick:bonus2 my current code is cat $F | awk -F"" how should i continue the code? Can i do something... (5 Replies)
Discussion started by: new2ss
5 Replies
Login or Register to Ask a Question