Remove all lines before a match with sed

I'm using sed to filter a list of files. I have a sorted list of folders and I want to get all lines after a specific one. To do this task I'm using the solution described here which works pretty well with any input I tried but it doesn't work when the match is on the first line. In that case sed will remove all lines of the input

Here it's an example:

$ ls -1 /
bin
boot
...
sys
tmp
usr
var
vmlinuz

$ ls -1 / | sed '1,/tmp/d'
usr
var
vmlinuz

$ ls -1 / | sed '1,/^bin$/d'
# sed will delete all lines from the input stream

How should I change the command to consider also the limit case when first line is matched by regexp?

BTW sed '1,1d' correctly works and remove the first line only.


Solution 1:

try this (GNU sed only):

sed '0,/^bin$/d'

..output is:

$sed '0,/^bin$/d' file
boot
...
sys
tmp
usr
var
vmlinuz

Solution 2:

This sed command will print all lines after and including the matching line:

sed -n '/^WHATEVER$/,$p'

The -n switch makes sed print only when told (the p command).

If you don't want to include the matching line you can tell sed to delete from the start of the file to the matching line:

sed '1,/^WHATEVER$/d'

(We use the d command which deletes lines.)

Solution 3:

you can also try with :

awk '/searchname/{p=1;next}{if(p){print}}'

EDIT(considering the comment from Joe)

awk '/searchname/{p++;if(p==1){next}}p' Your_File

Solution 4:

I would insert a tag before a match and delete in scope /start/,/####tag####/.