If you want to add a a line of text to the end of a file, and want to do the edit in place, use this syntax:
sed -i '$ a\This is some sample text.' /foo/bar
This is a repository of all of my Linux/Unix writings as well as useful tips and tricks for systems administration, engineering, and programming.
Showing posts with label sed. Show all posts
Showing posts with label sed. Show all posts
Thursday, November 3, 2011
Monday, July 25, 2011
sed with variables
Here is an example of how to get sed to replace values within files with variables:
for a in {1..12} ; do sed -e s/0/"$a"/g myConfig > myConfig$a ; done;
This is useful if you need to have an iterated set of config files, for instance.
for a in {1..12} ; do sed -e s/0/"$a"/g myConfig > myConfig$a ; done;
This is useful if you need to have an iterated set of config files, for instance.
In place editing of files quickly
Use the sed -i option for in place editing of files:
sed -i 's/foo/bar/g' FILENAME
It will replace all occurances of foo with bar within FILENAME
sed -i 's/foo/bar/g' FILENAME
It will replace all occurances of foo with bar within FILENAME
Friday, December 5, 2008
Rename files which match specific criteria
This renames all files within the current working directory from .exe to .exeold. This is useful if you are trying to rename any file extension from one to another.
for file in *.exe ; do mv $file `echo $file | sed 's/\(.*\.\)exe/\1exeold/'` ; done
for file in *.exe ; do mv $file `echo $file | sed 's/\(.*\.\)exe/\1exeold/'` ; done
Subscribe to:
Posts (Atom)