Sed Cookbook


The Linux sed command is a stream editor. What that means is basically that you can do a regex operation on each line of a file or a piped stream. You can also use perl like sed.

Sed does not use the extended regex syntax. Sed regex reminders:

  • You need a backslash before parens in a regex grouping
  • You refer to matched regex groups using \1, \2, etc.
  • The + regex operator does not work
  • Non-greedy quantifiers don’t work.  For example, .*? will not work
  • The output is printed to standard out by default.  You need the -i option if you want to edit a file with sed.

Remove all but the first column in a .tsv stream

sed 's/\([^\t]*\).*/\1/'

Edit a .tsv file by removing all but the first column

sed -i 's/\([^\t]*\).*/\1/'

Remove the first line of a stream

sed '1d'

Strip trailing whitespace from a file

sed -i -e 's/ *$//'

Recursively replace tabs with spaces

grep -Plr '\t' src/ | xargs sed -i 's/\t/  /g'

Replace @inheritDoc with @override after marking for edit

grep -l -r @inheritDoc java/com/benmccann | xargs p4 edit
grep -l -r @inheritDoc java/com/benmccann | xargs sed -i 's/\(.*\)@inheritDoc/\1@override/'

Replace @inheritDoc with @override in JS files after marking for edit

find java/com/benmccann -name '*.js' -print0 | xargs -0 grep -l @inheritDoc | xargs p4 edit
find java/com/benmccann -name '*.js' -print0 | xargs -0 grep -l @inheritDoc | xargs sed -i 's/\(.*\)@inheritDoc/\1@override/'
Be Sociable, Share!