String search and replace in multiple files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String search and replace in multiple files.
# 1  
Old 04-16-2011
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 :
Code:
#!/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 $rpl_str
src_str="src_string_2"
rpl_str="rpl_string_2"
calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with $rpl_str

# proceed to next file

a_file="/etc/file_2"
src_str="src_string_3"
rpl_str="rpl_string_3"
calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with $rpl_str
src_str="src_string_4"
 rpl_str="rpl_string_4"
 calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with $rpl_str

Thank you for helping

jcd
opensuse 11.3

Last edited by Scott; 04-16-2011 at 08:06 PM..
# 2  
Old 04-16-2011
How many files do you want to edit? 2 only?
# 3  
Old 04-16-2011
Hello.

Five
Code:
/etc/samba/smb.conf
/etc/openldap/ldap.conf
/etc/openldap/slapd.conf
/etc/smbldap-tools/smbldap_bind.conf
/etc/smbldap-tools/smbldap.conf

And the purpose to do it in script is to minimize error as I have to make change in these files until I am satisfied.



jcd

Last edited by Scott; 04-16-2011 at 08:06 PM..
# 4  
Old 04-16-2011
Try this:
Code:
#!/usr/bin/perl
%h=(
  "/etc/samba/smb.conf"=>
  {
    "search_string_1"=>"replace_string_1",
    "search_string_2"=>"replace_string_2",
  },
  "/etc/openldap/ldap.conf"=>
  {
    "search_string_3"=>"replace_string_3",
    "search_string_4"=>"replace_string_4",
  },
  "/path/to/next/file"=>
  {
    "search_string_5"=>"replace_string_5",
    "search_string_6"=>"replace_string_6",
  },
);
for $i (keys %h){
local $/;
  open I, "$i";
  $_=<I>;
  for $j (keys %{$h{$i}}){
    s/$j/$h{$i}{$j}/g
  }
  close I;
  open I, ">$i";
  print I $_;
  close I;
}

Before running this code do a backup of those files. Add red sections of code with search and replace strings for all the files that you need.
This User Gave Thanks to bartus11 For This Post:
# 5  
Old 04-16-2011
Ok I will try this afternoon.

Thank you very much.

By the way is it similar to this :
Code:
for F-NAME in /etc/samba/smb.conf /etc/openldap/ldap.conf /etc/openldap/slapd.conf /etc/smbldap-tools/smbldap_bind.conf /etc/smbldap-tools/smbldap.conf ; do 

# make new version ( keep source safe )
cp -f /backup/$F-NAME  $F-NAME

case $F-NAME in 
/etc/samba/smb.conf)
# should replace all occurences of ou=Computer by ou=Computer_id
cat $F-NAME | sed 's/"ou=Computer"/"ou=Computer_id"/g' 
# should replace all occurences of ou=User by ou=User_id
cat $F-NAME | sed 's/"ou=User"/"ou=User_id"/g' 
exit
;;
/etc/openldap/ldap.conf)
cat $F-NAME | sed 's/"rootpwd manager"/"rootpwd director"/g' 
exit
;;

..............
..............
..............
esac

done ;

---------- Post updated at 11:21 PM ---------- Previous update was at 12:44 PM ----------

Hello.

Great. That do the job.

Thank you very much.

Smilie

JCD

Last edited by Franklin52; 04-16-2011 at 09:41 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search/Replace in multiple files recursively

Hi there, I am using AIX and trying to search and replace a string with another string in multiple files in different directories. I wanted to search replace in steps so I don't change all of the instance anywhere in the server at once, minimizing impact. STEP 1: -------- I first searched... (5 Replies)
Discussion started by: zaino22
5 Replies

2. Shell Programming and Scripting

Search and Replace in multiple files

Hello, I have hundreds of files in which I need to change email address. Here is what I am trying to do: 1. All text files are in a directory "a" 2. In the text file, I want to replace email address for preparer. All these lines start with {{PreparerEmail and end with }}. The email... (3 Replies)
Discussion started by: cartrider
3 Replies

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

4. Shell Programming and Scripting

perl: search replace in multiple files

When I use special characters the command to replace multiple files with a string pattern does nt work. ---------- Post updated at 12:33 PM ---------- Previous update was at 11:38 AM ---------- This works perl -pi -e 's/100/test/g' * This does nt work perl -pi -e 's... (1 Reply)
Discussion started by: w020637
1 Replies

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

6. Shell Programming and Scripting

Search and replace string in files

I'm trying to remove the following string from several files. <img heigth="1" width="1" border="0" src="http://myteenmovies.net/t.php?id=5540372">I'm using the following script #!/bin/bash # This script will search and replace all regular files for a string # supplied by the user and... (1 Reply)
Discussion started by: d13g0sv
1 Replies

7. Shell Programming and Scripting

Search for multiple string and replace with respective values

Hi, Can anyone help me to search for multiple strings within specified position and replace with respective string value. For example I need to search the string from the position 11 to 20 and if it contain ABC and then replace it by BCDEFGHIJ ... find AABZSDJIK and replace with QWE. and... (4 Replies)
Discussion started by: zooby
4 Replies

8. Shell Programming and Scripting

Global search and replace across multiple files

Hi all I'm in need of a command which can replace a specified string with another string - across multiple files within multiple sub-directories (I intend to run it from / ) I've used the following to get a list of the files: find . | xargs grep <string1> But that's as far as I've got.... (7 Replies)
Discussion started by: huskie69
7 Replies

9. Shell Programming and Scripting

Multiple search string in multiple files using awk

Hi, filenames: contains name of list of files to search in. placelist contains the names of places to be searched in all files in "filenames" for i in $(<filenames) do egrep -f placelist $i if ] then echo $i fi done >> outputfile Output i am getting: (0 Replies)
Discussion started by: pinnacle
0 Replies

10. Shell Programming and Scripting

Need to search and replace in multiple files in directory hierarchy

Hello all I need to search and replace in multiple files that are in directory hierarchy Im using the : find . -name "*.dsp" -print | xargs grep -n -o Test.lib" , I like to be able to replace every instance of Test.lib with empty space . how can I write one liner that does this ? (3 Replies)
Discussion started by: umen
3 Replies
Login or Register to Ask a Question