Advertisement
zmatt

indentgrep

Oct 28th, 2022
1,261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.52 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use v5.28;
  4. use warnings qw( FATAL all );
  5.  
  6. @ARGV or die "Usage: $0 PATTERN [FILES...]";
  7.  
  8. my $pattern = shift @ARGV;
  9. $pattern = qr/$pattern/o;
  10.  
  11. my @where;
  12. my $wi = 0;
  13.  
  14. while(<>) {
  15.     s/\s*\z//;
  16.     length or next;
  17.     my $indent = /^(\s+)/ && $1;
  18.     while( @where && $where[-1] =~ /^$indent/ ) {
  19.         pop @where;
  20.         $wi = @where if $wi > @where;
  21.     }
  22.     push @where, $_;
  23.     if( /$pattern/ ) {
  24.         say $_ for @where[ $wi .. $#where ];
  25.         $wi = @where;
  26.     }
  27. } continue {
  28.     if( eof ) {
  29.         @where = ();
  30.         $wi = 0;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement