replace string in multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replace string in multiple files
# 1  
Old 10-22-2011
replace string in multiple files

Hi, I'm new to Unix. My understanding of Unix and its command is very limited.

I have about 1000 text files that have a word in it that I need to replace with a different word.

e.g.

a.txt has 1 line of txt: monday, tuesday, wednesday
b.txt has 1 line of txt: monday, tuesday, wednesday
c.txt has 1 line of txt: monday, tuesday, wednesday

I want to change all file contents to: monday, friday, wednesday

Is there a script I can create to do this? Thanks
# 2  
Old 10-22-2011
hey millsy, my knowledge is fairly limited as I don't use Unix much any more. 1 question - do you have Perl?
Anyway I will assume do don't . There is a command in Unix called sed. This should do what you want. Here's an example I copied off the web. I haven't checked it out but at a glance it looks like it will do what you want.

for file in $(grep -il "tuesday" *.txt)
do
sed -e "s/tuesday/friday/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done

The grep -il statement finds all instances of tuesday in files ending in txt (i = ignore case, l = only list the filename). The file names are passed to sed, which runs a regular expression to change all instances of tuesday to friday. Since sed doesn't overwrite a file, I redirected the output to a temp file and then renamed it back to the original file name.

Hope this helps
# 3  
Old 10-22-2011
hi frank, i tried that and got the following error:

replace_script: syntax error at line 2: `$' unexpected

do you know why i am getting this syntax error???


replace_script is the name of my text file that contains the code you gave me.

no i don't have perl.
# 4  
Old 10-22-2011
Code:
$ for i in *.txt ;do nawk '/tuesday/ {sub(/tuesday/, "friday")};1' $i > ${i}_tmp ; mv ${i}_tmp $i; done

# 5  
Old 10-22-2011
hey j, its not finding i. does this need to be declared?
# 6  
Old 10-22-2011
No need to declare the i .. What is the error u got when fired that code ..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Wanted to replace string in an .xlsx file in multiple ZIP Files

Hi , I am having a ZIP file containing an .xlsx file . Now i wanted to replace "GJ" to blank in the .xlsx file . I tried using the below code but not working , Please guide : #!/bin/bash log="/home/srikant/scripts/replacescriptFHO.log" date > $log echo "" >> $log echo initiating for FHO... (1 Reply)
Discussion started by: vipinmaster
1 Replies

2. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

3. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 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

String search and replace in multiple files.

Hello. I have five config files in /etc that I want to edit in one click for testing. I would like to make a script like this : #!/bin/bash # a_file="/etc/file_1" src_str="src_string_1" rpl_str="rpl_string_1" calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

replace string in multiple files, dir and subdir

Hello, I have a directory www with multiple directories. Every directory has site name with .htm, .html, .php files or sub directories with .htm, .php, .html file as example - www - sitename 1 - site 1 - sitename 2 - sitename 3 What I'm looking for is a... (7 Replies)
Discussion started by: andyjill
7 Replies

7. 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

8. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

9. UNIX for Dummies Questions & Answers

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies

10. UNIX for Dummies Questions & Answers

How to replace a string in multiple textfiles?

Hello I'm trying to replace a string in multiple text files using the tcsh shell. For example I've got some files called test1 test2 test3 etc. Each of them contains "Hello World". Now I want to replace each "Hello" with "Howdy" using sed and a foreach loop. I tried the following but it... (1 Reply)
Discussion started by: dwidmer
1 Replies
Login or Register to Ask a Question