Solved

Find & Replace

Posted on 2002-05-20
4
169 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
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot. I was surprised at how quick it processed the file!!

Cheers
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now