tags:

views:

13

answers:

1

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

The unix split command does 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?

A: 

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$
D.Shawley