Linux / UNIX Tech Support Forum
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 ...
|
|||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
| Sponsored Links | ||
|
|
|
|||
|
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
Code:
sed -i.bak 's/server3.nyc ; /server3.nyc server4.nyc;/' *.sh
__________________
Python tutorial | PHP manual | Bash Ref | Perl documentation | Awk Examples | Gawk | File Renamer Last edited by ghostdog74; 01-10-2010 at 08:09 PM. |
| The Following User Says Thank You to ghostdog74 For This Useful Post: | ||
jaysunn (01-10-2010)
| ||
|
||||
|
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 Last edited by jaysunn; 01-10-2010 at 09:05 PM. |
|
||||
|
Quote:
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 Last edited by nixcraft; 01-10-2010 at 09:08 PM. Reason: added an example |
|
||||
|
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 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]#
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]#
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 Last edited by jaysunn; 01-10-2010 at 09:31 PM. |
|
||||
|
@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 |
|
|||
|
i don't understand. please paste a copy of the permissions before and after here.
__________________
Python tutorial | PHP manual | Bash Ref | Perl documentation | Awk Examples | Gawk | File Renamer |
|
||||
|
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 |
![]() |
| Tags |
| bash , sed , shopt , shopt -s nullglob |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |