Sed

Sed is a steam editor

  • here a steam can be thought of as a body of text
  • sed works by reading text line-by-line into a buffer, and performing some pre-defined instructions (if such instructions are provided).
  • Sed helps us automate the same sort of tasks that we'd accomplish manually by opening a textfile and making manual, predictable edits.
  • like Vim, if we substitute without the g flag, then only the first occurrence on the line will substituted.
  • though / is the most common delimiter, we can use (almost) anything, like | or :. This is helpful if the / is part of the pattern we want to substitute.

Word-Boundary Expression

  • use \b to disallow partial-word matches
    • ex. we want to replace foo with kyle, but want to leave foobar alone: sed -i '' 's|\bfoo\b|kyle|g file.txt'

Find and Replace a pattern in all files of a tree

  • find and replace all occurrences of foo with bar within a directory tree
    • grep -rl 'foo' . | xargs sed -i .bk 's|foo|bar|g'
    • the first part gets a list of all files that have the pattern foo in it, then it pipes that list into the second part, which runs the sed substitution on each file
    • if we don't want to create a backup, then replace .bk with ''

Find and Replace a pattern in certain files

  • find and replace all occurrences of foo with bar in all .js files
    • find . -name "*.js" -exec sed -i '' s/foo/bar/g {} +

UE Resources

https://www.brianstorti.com/enough-sed-to-be-useful/ https://linuxize.com/post/how-to-use-sed-to-find-and-replace-string-in-files/


Children
  1. Formulas