Do something for lines that do or do not END 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 end with the word World.

~]# sed '/World$/d' /path/to/file.txt
How are you

 

Likewise, the following sed statement can be used to remove lines that do not end end with the word World.

~]# sed '/World$/!d' /path/to/file.txt
Hello World

 


Let's say you have a line of text that has some sort of delimitor, such as a forward slash.

/1First/2Second/3Third

 

The following sed statement can be used to change any alpha-numeric text after the last forward slash. In this example, the text after the last forward slash is replaced with Hello World.

~]# sed 's/[a-zA-Z0-9]*$/Hello World/g' /path/to/file.txt
/first/second/Hello World

 



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