LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 08-02-2005, 10:05 PM   #1
allelopath
Member
 
Registered: Jan 2003
Location: New Mexico
Distribution: SuSE 9.2 Professional, OpenSuSE 10.1 64 bit, Open SuSE 10.2, Debian Etch
Posts: 535

Rep: Reputation: 30
search/replace in many files


What is the best way to recursively search thru a directory structure and search and replace text in files?

For example, if I wanted to search thru all files with extension .txt and replace all occurences of the string "red" with "blue".
 
Old 08-02-2005, 10:21 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
And write it back out to the same file?
Code:
#!/bin/bash
SEARCH=red
REPLACE=blue


for file in `find . -name '*.txt'`
    do sed 's/$SEARCH/$REPLACE' < $file > $file.new
    mv $file.new $file
done
Test it on a copy first.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace a string1 with string2 in many files TroelsSmit Linux - Newbie 5 12-16-2004 02:04 PM
Search and Replace over multiple files The_Nerd Linux - Software 8 06-20-2004 07:59 AM
simple search and replace accross files/direcotries podollb Linux - Software 3 06-04-2004 09:30 PM
search for pattern in files and replace mizuki26 Linux - Newbie 3 01-04-2004 12:57 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 09:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration