Date created: Monday, January 27, 2025 11:09:33 AM. Last modified: Thursday, June 5, 2025 10:43:15 AM
'sed' - Notes
Replace
Search and replace, returning the changed output to stdout:
sed "s/aaa/bbb/" /path/to/input
Global search and replace, returning the changed output to stdout
sed "s/aaa/bbb/g" /path/to/input
Search and replace text between start "foo" and end "bar" markers, including the markers:
sed -E "/foo/,/bar/d" 0001_initial.py
Replace In Place
Search and replace within a file, without creating a backup file:
sed -i '' 's/search/replace/' /path/to/input
Search and replace within a file, and create a backup file:
sed -i '.bak' 's/search/replace/' /path/to/input
Delete
Delete only the first occurance of a pattern in a file
sed -i '0,/foo/{/foo/d;}' "$filename"
Multiple Replaces
One can concatenate multiple sed search and replace commands together:
sed 's/.*|--//;s/^@/\[/;s/:$/]/'
Previous page: 'screen' - Notes
Next page: 'sort' - Notes