Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find & Replace

Posted on 2002-05-20
4
Medium Priority
?
175 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

618 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