nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Replace Text String Multiple Files With SED

This is a discussion on Replace Text String Multiple Files With SED within the Getting started tutorials forums, part of the Linux Getting Started category; Hello Friends, Here I will show you a technique using SED to replace a text string with a new string ...


Go Back   nixCraft Linux Forum > Linux Getting Started > Getting started tutorials

Linux answers from nixCraft.


Getting started tutorials So much to read, so little time! If that is your problem, we have solution. Read our FAQ and tutorials to help you cut through the clutter of information overload. Only members of "contributors" group can post new tutorials. Other members can just reply to thread.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-10-2010, 06:33 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 598
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Replace Text String On Multiple Files With SED

Hello Friends,

Here I will show you a technique using SED to replace a text string with a new string in multiple files. The files that I will be altering are shell scripts that perform for loops to perform different tasks. When we add a new server to the cluster, we would have to manually open each shell script and the new server. This script will automate the process saving the admin precious time.

The shell script resides in /usr/local/bin on our staging server. There are 50 of them.

Code:
[root@staging1 bin]# ls
abc.sh   abcd.sh abcde.sh abcdef.sh abcdefg.sh abcdefgh.sh abcdefghij.sh
Lets look at the contents of a for loop script in this directory.
PHP Code:
[root@staging1 bin]# cat rsync_radiox_cluster.sh
#!/bin/bash

for server in "server1" "server2.nyc" "server3.nyc" ;        
do   

/
usr/bin/rsync  -e ssh -rlvtzgoD /dir1/dir2/dir3root@$server:/dir1/dir2/dir3/
done 
Now I will show you the sed script to add the new server to all of the shell scripts in /usr/local/bin.

PHP Code:
[root@staging1 bin]# cat sed_string_replace.sh 
#!/bin/bash
# Jason R. Ralph
# November 20th 2009

for scripts in $(find /usr/local/bin/ -name "*sh" awk -"/" '{print $NF}') ; do

cat $scripts sed 's/"server3.nyc"/"server3.nyc" "server4.nyc"/g' > /tmp/$scripts.tmp

cp 
-/tmp/$scripts.tmp  /usr/local/bin/$scripts

rm 
-/tmp
After you run the script you will see that all the shell scripts now have server4.nyc in them.

PHP Code:
[root@staging1 bin]# cat rsync_radiox_cluster.sh 
#!/bin/bash 

for server in "server1" "server2.nyc" "server3.nyc" "server4.nyc" ;         
do    

/
usr/bin/rsync  -e ssh -rlvtzgoD /dir1/dir2/dir3root@$server:/dir1/dir2/dir3
done 
You can modify the search criteria as well as the replace portion for your needs. You will have to alter this piece of the script. The search string is red. And the replace string is blue.

Code:
sed 's/"server3.nyc"/"server3.nyc" "server4.nyc"/g'
Please let me know if you have questions or concerns for this lesson.

HTH,

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 01-10-2010 at 06:38 PM.
Reply With Quote
  #2 (permalink)  
Old 01-10-2010, 08:06 PM
Member
User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 20 Times in 16 Posts
Rep Power: 6
ghostdog74 has a spectacular aura about ghostdog74 has a spectacular aura about ghostdog74 has a spectacular aura about
Default

1) your for loop with the find + awk command will break on files with spaces.
2) lose the cat. pass the file into sed directly

here's a version, using just shell and no external tool and assuming your *.sh files are not all over the place but just in /usr/local/bin

Code:
#!/bin/bash
shopt -s nullglob
for file in /usr/local/bin/*.sh
do
    while read -r line
    do
        case "$line" in
            *"for server in \"server1"* )
                line=${line%;}
                line="$line \"server4.nyc\" ;";;
        esac
        echo $line >> temp
    done < "$file"
    mv temp "$file"
done
or if you still want to use sed
Code:
sed -i.bak 's/server3.nyc ; /server3.nyc server4.nyc;/' *.sh

Last edited by ghostdog74; 01-10-2010 at 08:09 PM.
Reply With Quote
The Following User Says Thank You to ghostdog74 For This Useful Post:
jaysunn (01-10-2010)
  #3 (permalink)  
Old 01-10-2010, 08:27 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 598
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Hello,

I have been looking at the man page for shopt. However can you briefly explain what you are achieving with this line:

Code:
shopt -s nullglob

Thanks,

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 01-10-2010 at 09:05 PM.
Reply With Quote
  #4 (permalink)  
Old 01-10-2010, 08:55 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,697
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Quote:
Originally Posted by jaysunn View Post
Hello,

I have been looking at the man page for shopt. However can you briefly explain what you are achieving this this line:

Code:
shopt -s nullglob
Thanks,

Jaysunn
If there are no *.sh file in /usr/bin an error message will be displayed on screen and the script is not executed. It is kind of failsafe stuff. See
A simple example:
Code:
cd /tmp
touch x{1,2}.fb
var="*.fb"
echo $var
rm *.fb
var="*.fb"
# if there are no *.fb var will store *.fb itself
echo $var
shopt -s nullglob
var="*.fb"
echo $var
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help

Last edited by nixcraft; 01-10-2010 at 09:08 PM. Reason: added an example
Reply With Quote
  #5 (permalink)  
Old 01-10-2010, 09:28 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 598
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Hello Ghostdog,

I have attempted to use your script to add a new server to my for loop. However I am not seeing the scripts update in /usr/local/bin. Here is the example that I used.

Directory Contents:

Code:
[root@radio5 shelltest]# ls
script1.sh script2.sh script3.sh script4.sh script5.sh script6.sh
Each script contains something similar to this:
Code:
[root@staging1 shelltest]# cat remote_command.sh 
#!/bin/bash
#display usage
if [ -z "$1" ]; then
    echo -e usage: "Command \n" 
    exit
fi
  
for server in "server1.nyc" "server2.nyc" "server3.nyc"  ;        

do



echo -e "$server \n"
  ssh -l root $server "$1"
done
[root@staging1 shelltest]#
My goal is to add server4.nyc to the for loop in portion. I ran this:

Code:
root@staging1 shelltest]# cat tester.sh 
#!/bin/bash
shopt -s nullglob
for file in /usr/local/bin/*.sh
do
    while read -r line
    do
        case "$line" in
            *"for server in \"server3.nyc"* )
                line=${line%;}
                line="$line \"server3.nyc" "server4.nyc\" ;";;
        esac
        echo $line >> temp
    done < "$file"
    mv temp "$file"
done
[root@staging1 shelltest]#
My expected results are:

Code:
[root@staging1 shelltest]# cat remote_command.sh 
#!/bin/bash
#display usage
if [ -z "$1" ]; then
    echo -e usage: "Command \n" 
    exit
fi
  
for server in "server1.nyc" "server2.nyc" "server3.nyc" "server4.nyc" ;        

do

echo -e "$server \n"
  ssh -l root $server "$1"
done
[root@staging1 shelltest]#

The command completed successfully. However when checking the shell scripts, I did not see the new server. Am I running this incorrectly?

Please Advise.

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 01-10-2010 at 09:31 PM.
Reply With Quote
  #6 (permalink)  
Old 01-10-2010, 09:46 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 598
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

@ghostdog,

Your script worked great. It was my fault. I ran it from a test directory. However I was not looking at /usr/local/bin to verify since I tested in a different location.

One thing I noticed was my executable permissions were different on the changed scripts.


Sorry for the confusion. Any Idea why this could of occurred?

Thanks for all your help.

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #7 (permalink)  
Old 01-11-2010, 06:26 AM
Member
User
 
Join Date: Sep 2006
Posts: 66
Thanks: 0
Thanked 20 Times in 16 Posts
Rep Power: 6
ghostdog74 has a spectacular aura about ghostdog74 has a spectacular aura about ghostdog74 has a spectacular aura about
Default

i don't understand. please paste a copy of the permissions before and after here.
Reply With Quote
  #8 (permalink)  
Old 01-11-2010, 12:39 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,697
Thanks: 11
Thanked 243 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

mv may change the file permission. Are you deleting or moving exiting file before running mv? If so umask will affect it.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply

Tags
bash , sed , shopt , shopt -s nullglob


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
To find and replace in text file nashtech Shell scripting 5 01-31-2010 06:32 AM
[Solved] Read and replace text in html tags between 2 files aixjadoo Shell scripting 2 12-13-2009 05:20 PM
How to use sed to replace text between html tags raj Shell scripting 2 11-20-2009 07:24 PM
perl replace text kasimani CentOS / RHEL / Fedora 3 11-10-2009 07:56 PM
How to list and replace file of same name in multiple paths shchandu Shell scripting 1 08-27-2009 07:32 PM


All times are GMT +5.5. The time now is 12:08 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38