D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

Re: [LUG] Find & Replace across multiple files



Jonathan Melhuish wrote:
Anybody got any ideas on how to replace a long text string across multiple text files, using only a piece of paper, a pen, a piece of string and a linux box?


If all your files are in one directory, simple:


Using bash:
for file in REGEX; do sed -e 's/FIND_REGEX/REPLACE/g' $file > $file.new; mv $file.new $file; done
(that's just one line, please excuse any line wrapping)
Replacing REGEX, FIND_REGEX, REPLACE with appropriate values.


Combine sed with find if files are located in multiple directories. I don't have a script to hand that does this, but writing one shouldnt take long.

man find
man sed


Hope that helps. -- Tom Salmon


-- The Mailing List for the Devon & Cornwall LUG Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the message body to unsubscribe.


Lynx friendly