Solved

Find & Replace

Posted on 2002-05-20
4
173 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 100 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

717 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