0

I have a file with the same pattern several times. Something like:

   time
   12:00
   12:32
   23:22
   time
   10:32
   1:32
   15:45

I want to print the lines after the pattern, in the example time in several files. The number of lines after the pattern is constant. I found I can get the first part of my question with awk '/time/ {x=NR+3;next}(NR<=x){print}' filename But I have no idea how to output each chunk into different files.

EDIT

My files are a bit more complex than my original question. They have the following format.

4
gen
 C        -4.141000       -0.098000        0.773000
 H        -4.528000       -0.437000       -0.197000
 H        -4.267000        0.997000        0.808000
 H        -4.777000       -0.521000        1.563000
 4
 gen
 C        -4.414000       -0.398000        4.773000
 H        -4.382000       -0.455000       -4.197000
 H        -4.267000        0.973000        2.808000
 H        -4.333000       -0.000000        1.636000

I want to print the lines after

  4
  gen

EDIT 2

My expected output is x files x=# pattern. From my second example, I want two files:

 C        -4.141000       -0.098000        0.773000
 H        -4.528000       -0.437000       -0.197000
 H        -4.267000        0.997000        0.808000
 H        -4.777000       -0.521000        1.563000

and

 C        -4.414000       -0.398000        4.773000
 H        -4.382000       -0.455000       -4.197000
 H        -4.267000        0.973000        2.808000
 H        -4.333000       -0.000000        1.636000
ziulfer
  • 1,339
  • 5
  • 18
  • 30

3 Answers3

3

You can use this awk command:

awk '/time/{close(out); out="output" ++i; next} {print > out}' file

This awk command creates a variable out based on a fixed prefix output and an incrementing counter i which gets incremented every time we get a line time. All subsequent lines are redirected to this output file. Is is a good practice to close these file handles to avoid memory leak.

PS: If you want time line also in output then remove next in above command.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I didn't really get. Actually I want each chunk into a different file. – ziulfer Feb 27 '16 at 08:14
  • 1
    It is indeed creating a different file for each chunk. Did you test it? It is creating output file with the names as `output1`, `output2`, `output3` etc – anubhava Feb 27 '16 at 08:47
  • thanks @anubhava for the example file I gave it really worked. My problem is a bit more complex though. I edited my question. – ziulfer Feb 27 '16 at 17:36
  • ok can you also include new expected output by editing the question? – anubhava Feb 28 '16 at 06:06
0

The revised "4/gen" requirements are somewhat ambiguous but the following script (which is just a variant of @anubhava's) conforms with those that are given and can easily be modified to deal with various edge cases:

awk '
  /^ *4 *$/ {close(out); out=0; next}
  /^ *gen *$/ && out==0 {out = "output." ++i; next}
  out {print > out} '
peak
  • 105,803
  • 17
  • 152
  • 177
-1

I found another answer from anubhava here How to print 5 consecutive lines after a pattern in file using awk and with head and for loop I solved my problem: for i in {1..23}; do grep -A25 "gen" allTemp | tail -n 25 > xyz$i; head -n -27 allTemp > allTemp2; cp allTemp2 allTemp; done

I know that file allTemp has 23 occurrences of gen. Head will remove the lines I printed to xyzi as well as the two lines I don't want and it will output a new file to allTemp2

Community
  • 1
  • 1
ziulfer
  • 1,339
  • 5
  • 18
  • 30