Main menu:

Site search

Categories

Archive

How to bulk replace strings in a file on linux

Ever find that you need to do a bulk/mass replacement of strings in a file on a linux server?  I ran into a situation where I needed to change a website from using persistent connections to mysql to normal.  This involved changing mysql_pconnect to mysql_connect.  Using grep, There were about 5 files to change.

Of course this would not take to long just using emacs, but I wanted a quicker method.

The shell “replace” utility comes in handy.

replace from to [from to] … – file [file] …

In my case it was a simple

replace pconnect connect — *.php

in the appropriate directory.

Write a comment