DEV Community

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

Posted on β€’ Originally published at babakks.github.io on

3 2

Living in the Shell #14; sed (Text Stream Editor) (Part 1)

sed πŸ–ŠοΈ

Edits streams by applying commonly used modifications.

Add a new line a & i

cat some-file.txt | sed '3a This is a new line after the 3rd line' 
cat some-file.txt | sed '3i This is a new line before the 3rd line'
Enter fullscreen mode Exit fullscreen mode

Append a new line to the end $a

cat some-file.txt | sed '$a Now, this is the last line'
Enter fullscreen mode Exit fullscreen mode

Prepend a new line to the beginning 1i

cat some-file.txt | sed '1i Now, this is the first line'
Enter fullscreen mode Exit fullscreen mode

Replace one or more lines c

cat some-file.txt | sed '1-3c BOOM'
Enter fullscreen mode Exit fullscreen mode

Replaces lines 1 through 3, with 'BOOM'.

Delete one or more lines d

cat some-file.txt | sed '1-3d'
Enter fullscreen mode Exit fullscreen mode

Deletes lines 1 through 3.

Top comments (2)

Collapse
 
cubikca profile image
Brian Richardson β€’

These were some uses of sed I wasn't aware of, thanks. Still, the true power of sed must be acknowledged to be its replacement capabilities using regular expressions. For example, search and replace within a directory:

find . -name \*.js -exec cat {} | sed -e 's#replaceme#replacement#g' > output.txt

Collapse
 
babakks profile image
Babak K. Shandiz β€’

That's right. I'll cover some use cases of string/pattern substitution in Part 2.

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay