linuxfileunixawksunos

awk: too many output files 10 record number 12


Iam getting issue while trying to split the files. Can anyone help me with this ?

Sample content:

apple fruit
goa   fruit
tomato vegetable
tiger  animal 

Script:

 #!/bin/bash
 awk 'FNR==1 { hdr = $0; next }
    {
        if(!f[$2]) { print hdr >"file" $2".htm"; f[$2] = 1 }
        print >>"file" $2".htm"
    }' samplefile.txt

Solution

  • Could you please try following(untested since samples are not given), in case of sun o.s use /usr/xpg4/bin/awk OR /usr/xpg6/bin/awk(please refer comments in this answer for more details too).

    Seems like OP was using OLD broken awk where close function was not working of following solution, so I have suggested OP to use xpg awk(s), later Ed sir confirmed that too.

    #!/bin/bash
     awk 'FNR==1 { hdr = $0; next }
        {
            if($2 != prev){close(file)}
            file="file" $2 ".htm"
            if(!f[$2]) {print hdr > file; f[$2] = 1 }
            print >> file
            prev=$2
        }' samplefile.txt
    

    OR try:

    #!/bin/bash
     awk 'FNR==1 { hdr = $0; next }
        {
            file="file" $2 ".htm"
            if(file!=prev){close(prev)}
            if(!f[$2]) {print hdr > file; f[$2] = 1 }
            print >> file
            prev=file
        }' samplefile.txt