SED cheatsheet

Using sed to insert a line after a pattern

sed -i '/search_pattern/ainsert_line' filename

replace text with SED
The essential command: s for substitution

Replacing foo with foo_bar in a single file.

sed -i ‘s/foo/foo_bar/g’ somefile.module

find /home/bruno/old-friends -type f -exec sed -i ‘s/ugly/beautiful/g’ {} ;

A little script to find and replace multiple files at once:

#!/bin/bash
for fl in *.php; do
mv $fl $fl.old
sed ‘s/FINDSTRING/REPLACESTRING/g’ $fl.old > $fl
rm -f $fl.old
done

Leave a Reply

Your email address will not be published. Required fields are marked *