Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save each line in a text file as new file

Tags:

while-loop

awk

I have a tab delimited text file with 5 columns, and I'd like each row to be its own txt file that contains information from columns 2-5 and is named after column 1.

For example, my txt file has hundreds of rows similar to this:

sample1name_oligos primer forwardseq reverseseq sample1name

sample2name_oligos primer forwardseq reverseseq sample2name

I'd like to have a txt file named sample1name_oligos that look like this:

primer forwardseq reverseseq sample1name

and a txt file named sample1name_oligos that looks like this:

primer forwardseq reverseseq sample1name

I've tried two ways:

1. I found what I thought was the solution:

awk '{print substr($0,match($0,$2)) >> ( $1 ".txt" )}' filename

(from http://www.linuxquestions.org/questions/linux-newbie-8/how-to-save-each-line-from-textfile-as-new-file-889795/)

This worked for the test file I made (5 rows), but when I run it on my 100+ rows file I get the first 17 files out and then the error:

awk: File18.txt makes too many open files input record number 18, file myfile.txt source line number 1

I deleted row 18 and retried and got the same error. I deleted the first 20 lines and retried and got the same error.

2. From the same link, I tried

cat myfile.txt | while read LINE; do echo $LINE > "$LINE.txt"; done.

This made a file for each row that looked like this:

sample1name_oligos primer forwardseq reverseseq sample1name

and the file was named:

sample1name_oligos primer forwardseq reverseseq sample1name.

I'm not sure where to go from here. I'd appreciate any help. If it's not obvious, I have little Terminal experience so I'd also appreciate answers that explain what I'm missing.

Bonnie

like image 740
Bonnie Avatar asked Feb 09 '23 23:02

Bonnie


1 Answers

awk -F'\t' '$1!=prev{close(out); out=$1".txt"; prev=$1} {sub(/[^\t]+\t/,""); print > out}' file
like image 69
Ed Morton Avatar answered Feb 23 '23 13:02

Ed Morton