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:
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.
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
awk -F'\t' '$1!=prev{close(out); out=$1".txt"; prev=$1} {sub(/[^\t]+\t/,""); print > out}' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With