Sed

Regex

  • Does substitutions only once per line by default
  • Escape slashes with _
sed 's_/usr/local/bin_/common/bin_' <old >new

# Or use colons
sed 's:/usr/local/bin:/common/bin:' <old >new

Saving Matched Stuff with &

echo "123 abc" | sed 's/[0-9]*/& &/'

& is the value of whatever the regex is (excludes abc)

Add /2 at the end (along /g) to only match the second occurrence

Capture Groups

escape parenthesis with backslashes to create capture groups

.* is necessary to match any number of any character

echo "abc asdf" | sed 's/\([a-z]*\)*/\1/'

Flags

-i.new - modify the file in place and save it to file.new

Project-Wide Replacements

git ls-files | xargs sed -i 's/old_text/new_text/g'