Simple replace with sed command

Here's an example to scan/replace recursively:

grep -lr -e ' ' * | xargs sed -i 's/ //g'

Another sample:

echo "/tmp/testme.txt" | xargs sed -i 's/XXX/YYY/g'

Another sample, the dash i parameter makes it replace inline

sed -i 's|/bin/zip|/usr/bin/zip|g' testme.txt

Using sed with an input file:

sed -f sed.txt input.txt

input.txt

1 two three 1
four 5 six

sed.txt

s/1/one/g
s/5/five/

With the following sed commands

  • delete lines with [Control]
  • replace ? with a space
  • delete any lines that contain ONLY white space
  • delete any trailing white space
  • replace Word dash with a simple -
  • replace > < with ><
  • G tells sed to double space the output file

sed.txt

/\[Control\]/d
s/?/ /g
/^\s*$/d
s/:space:*$//
s/–/-/g
s/> </></g
G

Leave a Reply