Friday, July 29, 2022

[SOLVED] Remove line and the one before that matches pattern using sed

Issue

I have a file containing the following text:

>seq 1
GAA--ACGAA
>seq 2
CATCTCGGGA
>seq 3
GACG-CG-AG
>seq 4
ATTCCGTGCC

How can I delete the lines containing "-" and the ones before it using sed? Expected output:

>seq 2
CATCTCGGGA
>seq 4
ATTCCGTGCC

I have tried sed -e '/-/-1d' file, but I get sed: -e expression #1, char 4: unknown command: `-'

Thank you in advance.


Solution

Using sed

$ sed 'N;/-/d' input_file
>seq 2
CATCTCGGGA
>seq 4
ATTCCGTGCC


Answered By - HatLess
Answer Checked By - David Marino (WPSolving Volunteer)