Do something for lines that do or do not not BEGIN with a certain pattern in Linux bash

Home > Search
  by

Let's say file.txt contains the following text.

Hello
World
How
are
you?

 

The following sed statement can be used to remove lines that do begin with the letter H and then followed by anything.

~]# sed '/^H.*/d' /path/to/file.txt
World
are
you?

 

Likewise, the following sed statement can be used to remove lines that do not begin with the letter H and then followed by anything.

~]# sed '/^H.*/!d' /path/to/file.txt
Hello
How

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments