Split file by vector of line numbers

前端 未结 5 1665
礼貌的吻别
礼貌的吻别 2021-01-27 03:41

I have a large file, about 10GB. I have a vector of line numbers which I would like to use to split the file. Ideally I would like to accomplish this using command-line utilitie

5条回答
  •  长情又很酷
    2021-01-27 04:09

    Using awk:

    $ awk -v v="2 5" '       # space-separated vector if indexes
    BEGIN {
        n=split(v,t)         # reshape vector to a hash
        for(i=1;i<=n;i++)
            a[t[i]]
        i=1                  # filename index
    }
    {
        if(NR in a) {        # file record counter in the vector
            close("file" i)  # close previous file
            i++              # increase filename index
        }
        print > ("file" i)   # output to file
    }' file
    

    Sample output:

    $ cat file2
    4 5 6
    7 8 9 
    10 11 12 
    

提交回复
热议问题