Yesterday I ran into a situation where I had to edit 250,000 files, and of course I instantly thought of the Unix/Linux sed command. I knew what edit commands I wanted to run (simple swap/replace commands), but my bigger problem was how to edit the files in place.
A quick look at the sed man page showed that I needed to use the -i argument of the sed command:
-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied)
Since I did want to make a backup of each file, I included a filename extension, so my sed command looked a little like this example:
sed -i.bak -e's/2011/2012/' $filename
Note that I used the filename extension ".bak", and not just "bak". (You have to include the decimal if you want it, and I wanted my files to be named something like "foo.html.bak", not "foo.htmlbak".)
I just showed that example so you could see something simple. In reality what I did was to run a shell script that looked like this:
#!/bin/sh # create 'html_files.txt' like this: # find . -type f -name "*.html" > html_files.txt for file in `cat html_files.txt` do sed -i.bak -f my_commands.sed $file done
This shell script:
To help understand this example better, the sed commands contained in the file my_commands.sed looked like this:
s|<TITLE>|<title>| s|foo|bar|
(They were just a collection of sed swap/replace commands that I needed to run on every file.)
I hope these examples of how to use the Unix/Linux sed command to edit files in place has been helpful. For more information, take a look at the sed man page, or leave a note in the Comments section below.
Post new comment