How to change the replace the String in sub folders


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to change the replace the String in sub folders
# 1  
Old 01-10-2008
How to change the replace the String in sub folders

I have recursive sub directories.
I want to replace particular string in all the files in recursive sub directories.

E.g.

/abc/def/ghi/file1.txt
/abc/def/ijk/file2.txt
/abc/def/ghi/klm/file3.txt
/abc/xyz/mno/klm/file4.txt
/abc/rst/ghi/file1.txt

In above files I want to replace the string "pqr" with "uvw"

Correction in Title: How to replace the string in sub folders

I appreciate your help.

Last edited by bobbygsk; 01-10-2008 at 04:32 PM.. Reason: Wrong Title
# 2  
Old 01-10-2008
Code:
#!/bin/sh
find /abc -type f | while read filename
do
  sed 's/pqr/uvw/g' < $filename > ${filename}.modified
  mv ${filename}.modified $filename    # omit this line if you want to keep both versions
done

# 3  
Old 01-10-2008
If all the files are have similar names, file1.txt file2.txt, etc:
Code:
find /abc -name 'file*.txt -type f |
while read filename
do
    sed 's/pqr/uvw/g' $filename > tmp.tmp
    mv tmp.tmp $filename
done

check the output you get from find /abc -name 'file*.txt -type f BEFORE you run the whole script and ruin files you do not want changed.
# 4  
Old 01-10-2008
Gr8. It works.
Thanks for your help Smilie
# 5  
Old 01-10-2008
Thanks Smiling Dragon and jim
# 6  
Old 01-10-2008
Glad to be of help Smilie
# 7  
Old 09-30-2008
Hi
I want to replace control m charactes in all the files in recursive sub directories.When executing the below script ,i was getting

"files expression cannot replace with null"
and all files becomming zero byte files.

2.I want to drop recursively currernt directory and sub directories as well because all files are zero byte files .Any suggestions would be appericaited

Code:
E.g.

a/abc/file1.txt
a/abc/def/ijk/file2.txt
a/abc/def/ghi/klm/file3.txt
a/abc/xyz/mno/klm/file4.txt
a/abc/rst/ghi/file1.txt


find ./abc  -name "*"  -type f |while read filename
do
    sed 's/^M//g' $filename > tmp.tmp
    mv tmp.tmp $filename
done

Thanks,
Akil
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command to change add permissions for a new user to all files in all subfolders and folders

Hi there! I'm new to Unix and haven't done command line stuff since MS-Dos and Turbo Pascal (hah!), I would love some help figuring out this basic command (what I assume is basic). I'd like to add a User to the permissions of all files in a folder and all files in all subfolders, as well... (9 Replies)
Discussion started by: Janjbrt
9 Replies

2. Shell Programming and Scripting

Find and Replace from specific folders

Hello Experts, Any help is appreciated. I would like to find and replace a string in a specific file ( e.g abc.xml) only for the directories starting with "AB 1.0 DIR". I've 50 sub directories starting with "AB 1.0 DIR". And I would like find the file abc.xml in those sub-directories and... (4 Replies)
Discussion started by: builderj
4 Replies

3. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

4. Shell Programming and Scripting

Change all filenames under different folders...

Hi, all: I'd love to use shell script to change all filenames under different folders once for all: I've got over 100 folders, in each of them, there is a file named "a.ppm". I wanna change all these "a.ppm" to "b.ppm", and still . Visually, the directory structure looks like: and hope... (1 Reply)
Discussion started by: jiapei100
1 Replies

5. Shell Programming and Scripting

Search and Replace text in folders and Subfolders

Hi, I need help in writing a script to search a particular text in multiple files present in folders and sub folders and replace it with another string which also has special characters like '&', '|', etc.. I know sed command will be used to replace the text but i'm not sure how to use it for... (5 Replies)
Discussion started by: Asheesh
5 Replies

6. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Replace missing standard folders from home directories.

Hi, I want to develop a script to replace missing folders from home directories. These may have been deleted by the user. A standard home directory will have these folders in it and nothing else: Desktop, Documents, Downloads, Library, Movies, Music, Pictures, Public, Sites I also want to... (3 Replies)
Discussion started by: z399y
3 Replies

9. Shell Programming and Scripting

Find and replace files in multiple folders

Hi there, I would like to write a script to automate the copy and renaming of files in multiple dir. I have a generic file named s253e.prb and would like to copy this to multiple dir and rename it. Example: Dir is AL-M1 and the prb file name is AL-M1.prb. I would like to be able to... (6 Replies)
Discussion started by: lodey
6 Replies

10. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies
Login or Register to Ask a Question