Sed - need to replace a word by another in 82 files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed - need to replace a word by another in 82 files.
# 1  
Old 06-24-2009
Sed - need to replace a word by another in 82 files.

HI everyone Smilie

I looking for help to use sed.

I have 82 files. In each file I need to replace a word by another. All files are on the same directory.all files are texte files.

I need to keep the same file name for all file that will be modified.

Here is an exemple :

file name :
saturne_titan_20080609.txt
saturne_titan_20080709.txt ..etc

Files look like this inside :
cactus_sun_20080609.DIMG
cactus_sun_20080609.DIMG

I did try to replace the word cactus on this way

PHP Code:
sed 's/cactus/catusv2/g' 
but like this I have to work file by file like :

PHP Code:
cat saturne_titan_20080609.txt sed 's/cactus/catusv2/g' saturne_titan_20080609_new.txt 
and then delete the saturne_titan_20080609.txt and rename the saturne_titan_20080609_new.txt to saturne_titan_20080609.txt.

I am looking for an easy way to do everything in one command line. If possible Smilie

Thanks a lot if you can help me on this Smilie
# 2  
Old 06-24-2009
Code:
for file in `find . -name "saturne_titan_*.txt" -type f`
do
        sed "s/cactus/catusv2/g" $file > temp; mv temp $file;
done


please test first.
# 3  
Old 06-24-2009
Code:
#!/bin/ksh

for i in saturne_*.txt
do
  printf '%s/cactus/catusv2/g\n.\nwq!\n' | ex - ${i} 
done

# 4  
Old 06-24-2009
Dear anchal_khare

Your command line is working great. But the shell prompt me to :

PHP Code:
mvoverwrite ./saturne_titan_20080609.txt (yes/no)? 
I had to respond "yes".

Do you now a way on the command line to respond yes automatically ? or to prompt off this question ?

Thanks all for your help. Your so SUPER Smilie
# 5  
Old 06-24-2009
use "mv -f" instead of "mv"
# 6  
Old 06-24-2009
Dear vgersh99,

I try to script but it gave me an error :

PHP Code:
PsyNot an editor command 
I belivied that this is my fault has I have simplfied my request. I forgot to tell you that :

Inside of each file I found that some entries will not be modified as I do not need to :

here is what I mean : example of

saturne_titan_20080609.txt

Inside of this one we have :

TA_CLIR.DAT_RAP_20080609
cactus_sun_20080609.DIMG
cactus_sun_20080609.DIMG

each 82 files have this kind of exception entries. I have to say sorry for this. Smilie

Do you mind to help me with your script to make it work Smilie ? Thanks.
# 7  
Old 06-24-2009
sorry.
Code:
#!/bin/ksh

for i in saturne_*.txt
do
  printf '1,$s/cactus/catusv2/g\n.\nwq!\n' | ex - ${i} 
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

2. Shell Programming and Scripting

Replace a word using sed

Hi, I have the following line in file1 export NAME="NEW_NAME" I'm writing a shell script which reads the NEW_NAME from the user and replace in the file. I have used the following command for that read Name_replace sed -i 's/NEW_NAME/$Name_replace/' file1 but it is not... (2 Replies)
Discussion started by: Ananthdoss
2 Replies

3. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

4. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

5. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

6. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

7. Shell Programming and Scripting

Replace word with sed

Hi there.! I'm trying to make a script that corrects wrong spelling. I want to use sed to replace wrong word with the correct one, but this must be made in a while loop that reads the wrong word from file (with read line) and the correct one from another file. I can't find a way to run sed like... (3 Replies)
Discussion started by: spiii
3 Replies

8. Shell Programming and Scripting

sed search and replace word assistance...

Hi, I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file: 1.ejverything 2.bllown 3.maikling 4.manegement 5.existjing 6.systems My design currently takes input from the user, and... (2 Replies)
Discussion started by: mkfitzwilliams
2 Replies

9. Shell Programming and Scripting

replace word with using "sed" not work...

Hi, I have a xml text file with the following data, I would like replace F0</Number> to F</Number> only. i used sed to replace, but it not work!! anyone can help? <Number>11 20 03 22 23 21 91 00 F0</Number> <Number>12 20 03 20 99 21 91 20 F0</Number> <Number>10 21 03 21 78 21 92 27... (28 Replies)
Discussion started by: happyv
28 Replies

10. Shell Programming and Scripting

How to replace one word across too many files at once

I'm trying to replace the word "core3" with the word "core2" across too many scripts which they all contain that word "core3" (Shell & XML scripts) all at once. Is there easy way with "sed" to do it? Thanks Folks. (2 Replies)
Discussion started by: moe2266
2 Replies
Login or Register to Ask a Question