contentintech
Tools

Sed

Filter and transform line-oriented text without changing the original file.

Substitute

sed 's/old/new/' file.txtReplace the first match on each line
sed 's/old/new/g' file.txtReplace every match on each line
sed -E 's/[[:space:]]+/ /g' file.txtCollapse whitespace using extended regex
sed 's|/old/path|/new/path|g' file.txtUse a different delimiter for paths

Select lines

sed -n '5,10p' file.txtPrint a line range
sed -n '/ERROR/p' app.logPrint matching lines
sed '1d' file.txtDrop the first line from output
sed '/^$/d' file.txtDrop empty lines from output

Safe file workflow

sed 's/old/new/g' file.txt > updated.txtWrite transformed content to a separate file
diff -u file.txt updated.txtInspect the proposed change
sed -n '$p' file.txtPrint the last line
sed -n '1p' file.txtPrint the first line

Section navigation