DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Living in the Shell #15; sed (Text Stream Editor) (Part 2)
Babak K. Shandiz
Babak K. Shandiz

Posted on • Originally published at babakks.github.io on

Living in the Shell #15; sed (Text Stream Editor) (Part 2)

sed πŸ–ŠοΈ

Edits streams by applying commonly used modifications.

Replace substring s

cat some-file.txt | sed 's/me/you/g'
Enter fullscreen mode Exit fullscreen mode

Replaces all occurrences of "me" with "you".

Replace pattern s

echo "about\nabuse\namount" | sed 's/a\(\w*\?\)\w/\1/g'
Enter fullscreen mode Exit fullscreen mode
bou
bus
moun

\1 refers to the first captured group.

Transform to lowercase \L

echo "HELLO THERE\nHI THERE\nGOODBYE" | sed 's/H\w*/\L&/g'
Enter fullscreen mode Exit fullscreen mode
hello There
hi There
GOODBYE
  • Transforms words beginning with "H" to lowercase.
  • & is the place holder for "entire match".

Transform to uppercase \U

cat some-file.txt | sed 's/.*/\U&/g'
Enter fullscreen mode Exit fullscreen mode

Transforms all letters into uppercase.

Top comments (0)

Create an Account!

πŸ‘€ Just want to lurk?

That's fine, you can still create an account and turn on features like 🌚 dark mode.