title: select some lines as a group then give each line same label in same group in txt file
body: <p>I want to extract some lines of txt file between two patterns as different groups and give each line a same number as a label in each group.(last column input the group label ) for example, have data:</p>
  
  <pre><code>==g1
  a 1 2 3 4
  b 2 1 2 3
  ~~
  ==g2
  c 2...
  d 1...
  ...
  </code></pre>
  
  <p>I want to get output like</p>
  
  <pre><code>a 1 2 3 4 1
  b 2 1 2 3 1
  c 2 ...   2
  d 1 ...   2
  ...
  </code></pre>
  
  <p>I use python 3.7</p>
  
  <pre><code>with open('input.txt') as infile, open('output.txt', 'w') as outfile:
      copy = False
      for line in infile:
          #ng as the new label in each group
          ng=0
          if line.strip() == "start":
              copy = True
              continue
          elif line.startswith ('with'):
              copy = False
              continue
          elif copy:
              ng=ng+1
              line=line.rstrip('\n') +'\t'+ str(ng)+'\n'
              outfile.write(line)
  </code></pre>