Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split file by line count and rename based on column value

I have a file that needs to be split by every 4 lines. Each 4 lines look like:

sample_id   145 WORD    2847    42  301M    =   2086    -1062
ACAAAAAAGAAAAAATGAGTTACCGTACTGTCTGTGAGTGATGCATACTTTT
  |||||||||||||| ||| || ||||||  |||| |||||||||| ||||
TTAAAAAAGAAAAAATCAGTAACAGTACTGGATGTGGGTGATGCATATTTTT

so far I use

split -l 4 file.txt

I want to rename the output files so that it looks like sample_id.txt taken from the first column in the first line. How can I do this, is there an awk solution?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

awk 'NR % 4 != 1 { p = p ORS $0; next }
    function f() { if (length(p) && length(i)) print p > i ".txt" }
    { f(); p = $0; i = $1 }
    END { f() }' file
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading