Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing Text Globally? 5

Status
Not open for further replies.

tpbjr

MIS
Oct 8, 2004
120
US
Is there a way to do a global replace on a files content in a directory?

For example, I want to change ‘exports1’ to ‘exports’ in all files with an extension of ‘ksh’ (*.ksh).




Thank you for all your help

Tom
 
You might want to try it with an ed script like so

I'm just typing the following here, so it is untested!

cd /your/directory/name
for file in *.ksh
do
ed $file <<eof_ed
1,$ s/exports/exports1/g
w
q
eof_ed


HTH,

p5wizard
 
Thanks for get the help started.

I have the following that I produced as a test in a file named greplace.ksh, however, I get an error. Please see below. My test is slightly different than my original question but I would think the wild card would work on the extension portion as well.

cd $ME
for file in spool.*
do
ed $file <<eof_ed
1,$ s/$/(/g
w
q
eof_ed

ERROR MESSAGE RECEIVED:
greplace.ksh: syntax error at line 2 : `for' unmatched

I appreciate the help.

Thank you for all your help

Tom
 
You missed the done keyword:
man ksh

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
UNIX is not my expertise!
I have the following, however, it still does not work.

cd $ME
for file in spool.*
do
ed $file <<eof_ed
1,$ s/$/(/g
w
q
eof_ed
done



Thank you for all your help

Tom
 
This script works for a list so I think I have the done in the correct spot on the greplace.ksh script listed above?

cd $ME
for file in spool.*
do
ll $file
done

Thank you for all your help

Tom
 
Don't indent the "eof_ed" line; that needs to start in the first column to end the here document.


Alternately:

Code:
cd $ME
for i in *.ksh; do
    tmp="$i.tmp"
    sed s/exports1/exports/g >"$tmp"
    mv "$tmp" "$i"
done

The sed -i option is an even better method if your sed has it.
 
I really hate to drag this out but I am not having any luck.

I tried the following in a KSH file but it just sits at the cursor doing nothing. Any ideas?

cd $ME
for i in mytest.*; do
tmp="$i.tmp"
sed s/skip/script/g >"$tmp"
mv "$tmp" "$i"
done


I tried doing sed at the command line and it does the same ting!

sed s/skip/script/g > mytest.ksh

Thank you for all your help

Tom
 
Hi

As chipperMDW wrote, do not indent the here-document, or if you indent it, den indent with tab ( \t ) character and put a dash ( - ) after the less-then ( < ) signs :
Code:
cd $ME
for file in spool.*
do
    ed $file <<[red][b]-[/b][/red]eof_ed
[highlight #fdd]    [/highlight]1,$ s/$/(/g
[highlight #fdd]    [/highlight]w
[highlight #fdd]    [/highlight]q
[highlight #fdd]    [/highlight]eof_ed
done
Then you forgot to paste in the error message you got after the modifications.

And the [tt]sed[/tt] missies the input file :
Code:
sed [red]"[/red]s/skip/script/g[red]" "$i"[/red] >"$tmp"

Feherke.
 
Referring to the original question, why not something like:

[tt]perl -spi.bak -e 's/exports1/exports/g' *.ksh[/tt]

(This would save the original files as filename.ksh.bak)

Annihilannic.
 
Hi All,

Thank you so much for the help. I am going to go with the perl solution.

I am trying to get fance now, however, when I make my script accept parms the perl does not work. In addition, I took out the bak portion because once I get this thing working I will not need a backup of the files that I am doing the replace on.


if [ "$1." = "." ]
then
echo "Parm1 - Enter the path to do the replace in."
exit 1
fi
if [ "$2." = "." ]
then
echo "Parm2 - Enter text to replace."
exit 1
fi
if [ "$3." = "." ]
then
echo "Parm3 - Enter the replaced text."
exit 1
fi

cd $1
perl -spi -e 's/$2/$3/g' mytest.*



Thank you for all your help

Tom
 
When you use ' ' quotes it protects the string between them and prevents any variables from being substituted. Use " " quotes instead.

Annihilannic.
 
I want to thank everyone of you for helping with this solution. It works like a charm

greplace.ksh


if [ "$1." = "." ]
then
echo "Parm1 - Enter the path to do the replace in."
exit 1
fi
if [ "$2." = "." ]
then
echo "Parm2 - Enter text to replace."
exit 1
fi
if [ "$3." = "." ]
then
echo "Parm3 - Enter the replaced text."
exit 1
fi

cd $1
perl -spi -e "s/$2/$3/g" *.*

Thank you for all your help

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top