Bootstrap FreeKB - Bash (Scripting) - do something match does/doesn't end with pattern
Bash (Scripting) - do something match does/doesn't end with pattern

Updated:   |  Bash (Scripting) articles

Append to end of line

The $ character is used to do something from the end of a line. For example, let's say you have string "Hello World". With the most basic use, sed with $ can be used to append test to the end of the string. In this example, "Testing" is appended to the end of "Hello World".

~]# echo "Hello World" | sed "s|$| Testing|"
Hello World Testing

 


Replace end of line

The $ character is often used to replace text at the end of a line. In this example, lines ending with the word "World" are replaced with the word "Earth".

~]# echo "Hello World" | sed "s|World$|Earth|"
Hello Earth

 


Remove lines that end with

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

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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