Split file by vector of line numbers

前端 未结 5 1709
礼貌的吻别
礼貌的吻别 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:10

    Very slightly different from James's and kvantour's solutions: passing the vector to awk as a "file"

    vec="2 5"
    
    awk '
        NR == FNR {nr[$1]; next}
        FNR == 1 {filenum = 1; f = FILENAME "." filenum}
        FNR in nr {
            close(f)
            f = FILENAME "." ++filenum
        }
        {print > f}
    ' <(printf "%s\n" $vec) file
    
    $ ls -l file file.*
    -rw-r--r-- 1 glenn glenn 48 Jul 17 10:02 file
    -rw-r--r-- 1 glenn glenn  7 Jul 17 10:09 file.1
    -rw-r--r-- 1 glenn glenn 23 Jul 17 10:09 file.2
    -rw-r--r-- 1 glenn glenn 18 Jul 17 10:09 file.3
    

提交回复
热议问题