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?

>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

Leave a Reply