= SED - Stream Editor: Edit files via bash =
sed -i “s/wordtoreplace/wordtoreplacewith/” /path/to/file/file.txt
# -i = Edit File in place # s/ = s/regexp/replacement/ (search regexp and replace)
echo “teztz” | sed “s/z/-/” The above sed will *NOT* replace ALL instances of z with -. The following instead is required: echo “teztz” | sed “s/z/-/g” #The /g is required to replace all instances.
The following situation: A script outputs the following: /home/user1 /home/user2 /home/user3 Using Munin_Statistics, I want to be able to graph disk usage. The above string cannot be used to create a file for disk usage. I want to replace all instances of / with -. du -B 1 –max-depth=1 /home | sed “s/\//-/g” The trick is to escape the / with a \.
find . -type f -iname “*.php” -exec sed -i “s|www.website.ie/~user1|www.website2.ie/~user2|” {} \; #the pipe | is used to delimit sed
sed -i “/wordtodelete/d” text.txt #the /d is for delete instead of replace.
man sed