Delete Previous Line if Current Line Contains Particular Keyword

In UNIX delete previous line if current line contains particular keyword

Showing Answers 1 - 4 of 4 Answers


  • Jan 12th, 2017

sed -n /pattern/{x;d;};1h;1!{x;p;};${x;p;} <>

  Was this answer useful?  Yes


  • May 25th, 2017

grep -v "$(grep -B1 | head -1)"

  Was this answer useful?  Yes


  • Aug 31st, 2017

grep-B1 "particularword" filename | rm

  Was this answer useful?  Yes

Mahender Lal

  • Sep 2nd, 2017

prev_line=`grep -B1 "Keyword_to_search" filename | head -1` ; grep -v "$prev_line" filename
prev_line=`grep -B1 "Keyword_to_search" filename | head -1` ; grep -v "$prev_line" filename > newfile

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions