![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| shell script to find and replace string in multiple files | pharos467 | Shell Programming and Scripting | 11 | 01-15-2009 11:40 AM |
| Executing Multiple .SQL Files from Single Shell Script file | anushilrai | Shell Programming and Scripting | 3 | 04-07-2008 10:09 AM |
| Find and replace a string in multiple files | pharos467 | UNIX for Dummies Questions & Answers | 2 | 11-05-2007 11:47 PM |
| Find and replace files in multiple folders | lodey | Shell Programming and Scripting | 6 | 09-28-2007 02:00 AM |
| find and FTP multiple files in Korn Shell | lambjam | UNIX for Dummies Questions & Answers | 2 | 08-13-2007 10:50 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Find and Replace in multiple files (Unix Shell script)
hi guys,
Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin Last edited by jatins_s; 08-20-2002 at 02:27 AM.. |
|
|||||
|
If I understand what you're asking, this code should do what you want (ksh):
cd your_pathname for i in `lx`; do FOUND=`grep ABCD $i` if [ "$FOUND" ] then TMP=./tmpfile_$$ sed -e 's/ABCD/DCBA/g' < $i > $TMP mv $TMP $i else echo "The file $i does not include the word ABCD" fi done This script reads each file in your_pathname, searches for the string ABCD, and either replaces it with DCBA or alerts you that the string ABCD was not found. Last edited by oombera; 08-21-2002 at 10:49 AM.. |
|
|||||
|
I would bet that peter's is faster...
You could make it a split second faster by getting rid of the `ls *` though... If could just be: for y in * since the shell will expand the * into all of the file names... If you had directories in there, you'd want to use "find" to get only the files... |
|
|||||
|
remember you are redirecting output to a file and then moving the file back to the original
what if the file sizes are of the order of tens of MBs. secondly, the shell script would compulsorily do it for all the files in the directory. but while using a file, you can customize your renaming process. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|