Extract lines between 2 tokens in a text file using bash

后端 未结 7 680
死守一世寂寞
死守一世寂寞 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 (<STDIN>) {
       chomp;
       if (/token 1/) {
          $recording = 1;
       }
       else if (/token 2/) {
          $recording = 0;
       }
       else if ($recording) {
          push @results, $_;
       }
    }
    
    0 讨论(0)
提交回复
热议问题