Linux sed FAQ: How do I run a Linux sed command from the command line?
I usually only use the Linux sed command in sed scripts, but today I needed to do something much easier than normal, and as I thought about how to run a sed command from the Linux command line, I had to pause for a few moments. Finally I remembered the sed command line syntax, and it looks like this:
sed 's/THE_DATE/2010-07-11/' < sitemap-orig.xml > sitemap.xml
This sed command can be read like this:
- Read from the file named sitemap-orig.xml
- Write output to the file named sitemap.xml
- As you read every line from the input file, change every instance of the string
THE_DATEto the string “2010-07-11”, and then write each line to the output file.
If you haven’t used the Linux sed command before, it's important to note that the file sitemap-orig.xml is not modified; I just read from it, and write to the output file. (As a word of warning, whatever you do, don’t try to read from the same file you’re writing to; the Linux shell will clobber your file before you have a chance to read it.)
Linux sed command in a shell script
In my case, I’m actually running this sed command from a Unix shell script, so what I really do is (a) generate today's date in the format I want, and (b) then I run this Linux sed command. The lines of code from my shell script look like this:
# create the date in the desired format (2010-07-11) thedate=`date +"%Y-%m-%d"` # put the date in the sitemap.xml file sed "s/THE_DATE/$thedate/" < sitemap-orig.xml > sitemap.xml
The shell script variable $thedate contains the date in the format I want, and then I run the Linux sed command to replace every instance of the string THE_DATE with today’s date.
I hope this little Linux sed command example is helpful. If you're looking for more powerful sed command examples, just search my website for "sed".

