Home > Archive > PERL Beginners > July 2004 > a sed equivelent
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Jerry M. Howell II 2004-07-28, 8:56 pm |
| hello all,
I'm just beginning to work with sed in shell scripts I was wondering
if anyone has a simmilar script in perl they can send my way or what the
perl equivelent of the sed /s would be. Here is the coresponding shell
script if it'll help you understand what I'm trying to acomplish.
#!/bin/sh
# Need some information from you
echo "what word or string are we looking for?"
read old
echo ""
echo "what do you wish to replace it with"
read new
for script in *
do
sed 's/'$old'/'$new'/g' <$script> outfile
mv outfile $script
done
sometimes this script works and sometimes it just clears the file out
leaving it empty. I need a viable alternative but don't even know where
to start. Thanks for any help you can give me.
--
Jerry M. Howell II
email admin: usalug.org
sys admin: hostbyk
| |
| Bob Showalter 2004-07-28, 8:56 pm |
| Jerry M. Howell II wrote:
> hello all,
>
> I'm just beginning to work with sed in shell scripts I was
> wondering if anyone has a simmilar script in perl they can send my
> way or what the perl equivelent of the sed /s would be. Here is the
> coresponding shell script if it'll help you understand what I'm
> trying to acomplish.
>
> #!/bin/sh
>
> # Need some information from you
>
> echo "what word or string are we looking for?"
> read old
> echo ""
> echo "what do you wish to replace it with"
> read new
> for script in *
> do
> sed 's/'$old'/'$new'/g' <$script> outfile
The perl equivalent is very similar:
perl -pe "s/$old/$new/g" $script >outfile
> mv outfile $script
> done
>
> sometimes this script works and sometimes it just clears the file out
> leaving it empty. I need a viable alternative but don't even know
> where to start. Thanks for any help you can give me.
The problem is that you're not checking to see if the sed (or perl) was
successful. If you supply a bogus pattern to sed for example, outfile will
be empty. You then overwrite the input file with the (now empty) file.
| |
| Ramprasad A Padmanabhan 2004-07-28, 8:56 pm |
| This should do your work, as long as your strings dont have any special
chars.
perl -pli.BAK -e 's/OLDSTRING/NEWSTRING/g' FILENAME
(You can also use it for multiple files and will create a .BAK file
incase you want to revert )
HTH
Ram
On Mon, 2004-07-26 at 17:21, Jerry M. Howell II wrote:
> hello all,
>
> I'm just beginning to work with sed in shell scripts I was wondering
> if anyone has a simmilar script in perl they can send my way or what the
> perl equivelent of the sed /s would be. Here is the coresponding shell
> script if it'll help you understand what I'm trying to acomplish.
>
> #!/bin/sh
>
> # Need some information from you
>
> echo "what word or string are we looking for?"
> read old
> echo ""
> echo "what do you wish to replace it with"
> read new
> for script in *
> do
> sed 's/'$old'/'$new'/g' <$script> outfile
> mv outfile $script
> done
>
> sometimes this script works and sometimes it just clears the file out
> leaving it empty. I need a viable alternative but don't even know where
> to start. Thanks for any help you can give me.
>
> --
> Jerry M. Howell II
>
> email admin: usalug.org
>
> sys admin: hostbyk
>
|
|
|
|
|