replacing a string in multiple subdirs to a new string??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing a string in multiple subdirs to a new string??
# 1  
Old 09-30-2009
replacing a string in multiple subdirs to a new string??

I have following set of dirs:

/dir1/dir2/subdir1
file1
file2
/dir1/dir3/subdir1
file4
file5
/dir1/dir4/subdir1
file6
file7

All of these files have a common string in them say "STRING1", How can I replace "STRING1" to "STRING2" for all files.

Thanks in advance.

Smilie
# 2  
Old 09-30-2009
If you mean all the files in those subdirectories:

find /dir1 -type f -exec sed -i 's/STRING1/STRING2/g' '{}' ';'

If you mean specifically those and only those, just list them after sed like so:

sed -i s/STRING1/STRING2/g /dir1/dir2/subdir1/file[12] /dir1/dir3/subdir1/file[45] /dir1/dir4/subdir1/file[67]
# 3  
Old 09-30-2009
Thanks for your reply.

It says "illegal option -i", we are on AIX V5.3

---------- Post updated at 09:33 PM ---------- Previous update was at 04:14 PM ----------

Any update on this please??

Smilie
# 4  
Old 10-01-2009
Your sed version doesn't support the inline option. Try this:

Code:
find /dir1 -type f | xargs -i bash -c "sed 's/STRING1/STRING2/g' {} > {}.bak; mv {}.bak {}"

Be careful with this as there is no way back. Do some tests first. You can also change the mv command by cp to keep a backup of original file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a string

Hi All, I have a many folders in a directory under which there are many subdirectories containing text files containing the word "shyam" in them.I want all the files in all the directories containing "shyam to "ram" ?? sed "s/shyam/ram/g" does it ??But anyone can help me with the script ?? ... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

2. Shell Programming and Scripting

Remove multiple lines from a particular string to particular string

Hi, I have a file containing the DDLs of tables in a schema. From that I need to remove all the lines from a starting string till a specific string. Here is an example. File1.txt ------------- CREATE TABLE "SCHEMA1"."LKP11_TBL_USERS" ( "ID" NUMBER(8,0) NOT NULL ENABLE, "USER_ID"... (3 Replies)
Discussion started by: satyaatcgi
3 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

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

5. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

6. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

7. Shell Programming and Scripting

Replacing string in multiple files

Hi, I need to replace the string 'abcd' with 'xyz' in a file sample.xml This sample.xml is also present in the subdirectories of the current directory. Eg, If I am in /user/home/ the sample.xml if present in /user/home/ /user/home/folder1/ /user/home/folder2/... (3 Replies)
Discussion started by: arulanandsp
3 Replies

8. Shell Programming and Scripting

Combine multiple string into 1 string group by certain criteria

Hi all, I am newbie in unix. Just have some doubts on how to join multiple lines into single line. I have 1 file with following contents. R96087641 HostName-kul480My This is no use any more %% E78343970 LocalPath-/app/usr/SG (Blank in this line) %% E73615740... (4 Replies)
Discussion started by: whchee
4 Replies

9. Shell Programming and Scripting

renaming multiple files while replacing string

hi, i've found a few examples of scripts to do this but for some reason can't get them to work properly. basically i have some dirs with a few hundred files mixed in with a bunch of other files that were made with a typo in part of them. long-file-names-tyo-example.ext want to be able... (2 Replies)
Discussion started by: kevin9
2 Replies

10. Shell Programming and Scripting

string replacing

hii, i need a unix command which replaces all occurrences of a substring within a string with another substring. My solution: string="plalstalplal" sub1="al" sub2="mlkl" echo sed 's/$s2/$s3/g' < s1 > p i want to know how to read the variables s2 and s3.. thaks a lot bye (1 Reply)
Discussion started by: priya_9patil
1 Replies
Login or Register to Ask a Question