开发者

Deleting N lines from every start point

How would I delete 开发者_运维技巧the 6 lines starting from every instance of a word i see?


I think this sed command will do what you want:

sed '/bar/,+5d' input.txt

It removes any line containing the text bar plus the five following lines.

Run as above to see the output. When you know it is working correctly use the switch --in-place=.backup to actually perform the change.


This simple perl script will remove every line that containts word "DELETE6" and 5 consecutive lines (total 6). It also saves previous version of file in FILENAME.bak. To run the script:

perl script.pl FILE_TO_CHANGE

#!/usr/bin/perl

use strict;
use warnings;

my $remove_count = 6;
my $word = "DELETE6";

local $^I = ".bak";
my $delete_count = 0;
while (<>) {
    $delete_count = $remove_count if /$word/; 
    print if $delete_count <= 0;
    $delete_count--;
}

HTH


perl -i.bak -n -e '$n ++; $n = -5 if /foo/; print if $n > 0' data.txt


perl -ne 'print unless (/my_word/ and $n = 1) .. ++$n == 7'

Note that if my_word occurs in the skipped-over lines, the counter will not be reset.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜