Sed
From Wiki
Contents |
[edit]
SED - Stream Editor: Edit files via bash
[edit]
Example of replacing text
sed -i "s/wordtoreplace/wordtoreplacewith/" /path/to/file/file.txt # -i = Edit File in place # s/ = s/regexp/replacement/ (search regexp and replace)
[edit]
Replace all instances of text in a line
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.
[edit]
Replace a Forward Slash (/) using sed
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 \.
[edit]
Other information about sed
man sed
