Berkeley CSUA MOTD:Entry 13379
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2004/4/26 [Computer/SW/Unix, Computer/SW/Languages/Perl] UID:13379 Activity:nil
4/26    How do I use sed or perl, etc. to delete all lines from a file
        before or after some pattern.  For example, in vi, I could type
        /<pattern>dG or 1Gd/<pattern>.  But how do I put this into a
        script?
        \_ I believe sed '/<pattern>/d' will do what you want.
           \_ No, that only deletes the line with the pattern.  It doesn't
              do anything to the lines before or after it.
              \_ Sorry.  Misread the question.
        \_ in perl you want something like:
           $skip = 0;
           open(INFILE,$infile);
           open(OUTFILE,">$outfile");
           while (<INFILE>) {
                $skip = 1 if (/[pattern]/);
                print OUTFILE $_ unless ($skip);
           }
           close (INFILE);
           close (OUTFILE);
           \_ one line form:
              perl -ni -e'$skip=1 if /PATTERN/; print unless $skip' FILENAME
                Change "unless" to "if" if you want to print after the PATTERN
              \_ Thanks.
        \_ manning sed should reveal addresses. you can do:
            sed '/moo/,$d'
           to delete all lines after finding a line that matches the regexp
           /moo/. -ali