Posted: . At: 10:46 PM. This was 10 years ago. Post ID: 7665
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Basic text filtering with sed. Very useful when you are manipulating text files.

This example uses sed to replace the beginning word of a sentence.

Administrator@WIN-EM8GK0ROU41 ~
$ echo "this is a line of text." | sed "s/this/This/gi;"
This is a line of text.

This is a better way to capitalize the first letter of every word in the sentence. I found this tip here.

Administrator@WIN-EM8GK0ROU41 ~
$ echo "this is a line of text." | sed -r 's/\<./\U&/g'
This Is A Line Of Text.

And here is how to only capitalize the first letter in the sentence.

Administrator@WIN-EM8GK0ROU41 ~
$ echo "this is a line of text." | sed "s/^./\u&/"
This is a line of text.

Here is a way to use sed without needing cat. I used this to nicely format a HTML file that had no newlines in it and was a mess when printed to the terminal.

jason@jason-Lenovo-H50-55:~/Downloads$ sed "s/>/>\n/gi;" index.html

Here is a related command: this will print your gateway IP address.

jason@jason-Lenovo-H50-55:~/Downloads$ curl ipinfo.io/ip

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.