How to conditionally delete the first line only with sed when it matches?

Can I use sedto check the first line of the command output (on stdout) and delete this very first line if it matches a specific pattern?

Let's say the command output looks something like this:

"AB"
"CD"
"E"
"F"

I want him to become:

"CD"
"E"
"F"

But when the first line "GH", I do not want to delete the line.

I tried this, but it does not work:

<some_command> |sed '1/<pattern>/d'

About me said:

sed: 0602-403 1/<pattern>/d is not a recognized function.

I just want to use sed to process the first line, leaving the other lines intact.

What is the correct syntax here?

+5
source share
4 answers

This might work for you:

sed -e '1!b' -e '/GH/!d' file
+6
source

You want to refer to the 1st line, then say delete:

$ sed '1 d' file

- , , .

:

$ sed '0,/pattern/ d' file
+4

Is this what you want:

$ sed '1{/"GH"/!d}' file
+2
source
sed '1{/<pattern>/{/GH/!d}}' input

An error in the expression can be fixed as follows:

sed '1{/<pattern>/d}' input
+2
source

All Articles