开发者

Split and add header on the command line

What's the easiest way to split a file and add a header to each section?

The unix split command does 开发者_StackOverflow社区everything that I need minus being able to add a header.

Any easy way to do it with existing tools before I script it up?


It is probably easiest to do this in either awk or perl. If you aren't processing much data, then using a simple shell script to post-process the output of split is probably fine. However, this will traverse the input more than once which can be a bottleneck if you are doing this for any sort of online data processing task. Something like the following should work:

bash$ cat input-file | awk '
  BEGIN {
      fnum = 1
      print "HEADER" > fnum
  }
  {   if ((NR % 10) == 0) {
          close(fnum)
          fnum++
          print "HEADER" > fnum
      }
      print >> fnum
  }
'
bash$ wc -l input-file
     239 input-file
bash$ ls
1                19               6
10               2                7
11               20               8
12               21               9
13               22               input-file
14               23
15               24
16               3
17               4
18               5
bash$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜