Extract lines between 2 tokens in a text file using bash

后端 未结 7 756
死守一世寂寞
死守一世寂寞 2020-12-05 07:26

i have a text file which looks like this:

random useless text 
 
para1 
para2 
para3 
 
random us         


        
7条回答
  •  盖世英雄少女心
    2020-12-05 08:33

    For anything like this, I'd reach for Perl, with its combination of (amongst others) sed and awk capabilities. Something like (beware - untested):

    my $recording = 0;
    my @results = ();
    while () {
       chomp;
       if (/token 1/) {
          $recording = 1;
       }
       else if (/token 2/) {
          $recording = 0;
       }
       else if ($recording) {
          push @results, $_;
       }
    }
    

提交回复
热议问题