Sed(1) one-liners(sed.sourceforge.net) |
Sed(1) one-liners(sed.sourceforge.net) |
My most useful one-liner:
sed -i "s/pat/rep/g" file
It means replace in-place all matches of pat with rep in file.To make a backup of old file use -i.bak instead of -i where .bak would be the suffix of the backup file.
I have heard that it's GNU sed option only. (But can't currently say if it's 100% true.)
See "UNIX Style, or cat -v Considered Harmful" by Rob Pike (http://harmful.cat-v.org/cat-v/) for a longer discussion of this.
But I think it is in GNU sed for a reason -- it is a practical shortcut. The only thing I use sed for is to do search and replaces. If the command spanned 2 lines or had pipes and redirects a lot of people just wouldn't remember it.
If I want to edit a stream I use sed
perl -e 's/pat/rep/g' -p -i file
-i.bak instead of -i works the same to create backups. sed 's/blah/foo/' file | sponge files stands for stream
there is no s in ed
"...Many other commands could also use a -o command. For example, sed could edit a file in place: ... It would be impractical to modify all such commands to add the option. Furthermore, it would be bad design: it is better to centralize functions, as the shell does with the > operator."