Solved

Find & Replace

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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). …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

821 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