How do I delete a matching line and the previous one?

前端 未结 4 908
说谎
说谎 2020-12-11 05:13

I need delete a matching line and one previous to it. e.g In file below I need to remove lines 1 & 2.

I tried \"grep -v -B 1 \"page.of.\" 1.txt and I

4条回答
  •  余生分开走
    2020-12-11 05:47

    Not too familiar with sed, but here's a perl expression to do the trick:

    cat FILE | perl -e '@a = ;
                        for( $i=0 ; $i <= $#a ; $i++ ) { 
                         if($i > 0 && $a[$i] =~ /xxxx/) { 
                           $a[$i] = ""; 
                           $a[$i-1] = "";
                         }
                        } print @a;'
    

    edit:

    where "xxxx" is what you are trying to match.

提交回复
热议问题