Cheat sheet: sed

This post is a lose collection of useful applications of sed.

Grepping

The following commands are equivalent:

The -n flag makes sed print only the matching lines.

Replacing Pathnames

The following command replaces the placeholder CURRENT_WORKING_DIR with the current working directory inside file.txt:

 Replacing in a whole Directory Tree

We can use find to filter all files in a directory (tree) and then hand these files to sed. The following command prints out all true files (not directories) and lets sed replace the term et cetera with etc.

While we could also use the -exec flag of find this separation of concerns makes it easy to dry-run the modifications by exchanging the sed command. The flag -n reduces the output to the matches only and the command p triggers the printing of matching lines.

 Tutorials

  • [1] Colorful tutorial by grymoire

Leave a Reply

Your email address will not be published. Required fields are marked *

Please type the characters of this captcha image in the input box

Please type the characters of this captcha image in the input box