How to split a file and keep the first line in each of the pieces?

前端 未结 12 789
-上瘾入骨i
-上瘾入骨i 2020-12-07 18:28

Given: One big text-data file (e.g. CSV format) with a \'special\' first line (e.g., field names).

Wanted: An equivalent of the cor

12条回答
  •  情书的邮戳
    2020-12-07 18:59

    You can use [mg]awk:

    awk 'NR==1{
            header=$0; 
            count=1; 
            print header > "x_" count; 
            next 
         } 
    
         !( (NR-1) % 100){
            count++; 
            print header > "x_" count;
         } 
         {
            print $0 > "x_" count
         }' file
    

    100 is the number of lines of each slice. It doesn't require temp files and can be put on a single line.

提交回复
热议问题