?
Solved

Find & Replace

Posted on 2002-05-20
4
Medium Priority
?
174 Views
Last Modified: 2013-12-15
Is it possible to find a string within thousands of files in a directory, and replace them with another string?

I.E In the /home/list directory, there are 1,00 files, and within each of those files, there is the string "This is a test". Would it be possible to replace that string within all files to "Testing String" ?

Cheers
0
Comment
Question by:choccarlm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 400 total points
ID: 7021420
Sure, that can be done. If you have thousands of files it would be best to create a small script file (say called repl-string) containing:

#!/bin/sh
cat $1 | sed -e "s/This is a test/Testing String/" > $1

and make the script executable with 'chmod +x repl-string'. Then use find to feed the file names to the script, like:

> cd /home/list
> find . -type f -exec /path-to/repl-string {} \;
0
 

Author Comment

by:choccarlm
ID: 7021626
sorry, but how do I feed the filenames to the script? I'm a little bit confused by the example you gave me.

Sorry
0
 
LVL 40

Expert Comment

by:jlevie
ID: 7021896
The script is invoked like '/path-to/repl-string some-file'. the path-to will depend on where you create the script file. Per the question, the files to be changed happen to be in /home/list and if you create the script file in say /home/my-account, you could invoke the script with /home/my-account/repl-string some-file.

Since there are lot's of files to process we need a way to get the file names and invoke the script on each name. The 'find' command does that nicely. If you want to see what it does execute:

> cd /home/list
> find . -type f

and you'll see a list of file names in the current directory and any sub-dirs it might have. To process each file I used find, like above, and told it to execute repl-string for each file name. The {} in the command is replaced by each name that find finds.

For a smaller list of files one could use a shell for loop, like:

> for f in *; do /path-to/repl-string $f; done

However that won't handle subdirs and there can be a problem in if there's a really big list of files in the directory. So using find is safer.

When using find or a for loop it's important to not have the script in the directory with the files to be modified. If you put the script there it'll be found and processed and the results won't be what you expect. Some files will get changed and others won't because the script will modify itself at some point.
0
 

Author Comment

by:choccarlm
ID: 7023602
Thanks a lot. I was surprised at how quick it processed the file!!

Cheers
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question