Ano-Tech Computers
Enter keyword:

Fast search & replace using sed
Problem:
How to perform a search and replace on multiple files in Linux?
 
Solution:
Use the "sed" command with a regular expression. For example, let's say you have a whole bunch of .css files where you need to replace every occurrance of the word "serif" with "sans-serif". Type the following:

"sed s/serif/sans\-serif/ *.css"

This command will read all the input files, apply the regex and print the output stream to the console. Now review the change to see if your regular expression worked as expected.

If you're satisfied, execute "sed" again, but this time with the -i option. This option makes sed actually write the modified stream back to each input file:

"sed -i s/serif/sans\-serif/ *.css"

For more advanced use of sed, please refer to the man page.
 
Discuss this solution
Did this article solve your problem? Yes No Did not apply

We welcome anyone who is willing to contribute to this public knowledge base, contact siteadmin@atc.no if you have information you would like to share. The idea is not to replace the commercial support sites, but to publish those hard-to-find solutions you've found yourself looking for over and over again.

Show all articles