Simple File Editing Question;

Simple File Editing Question;
About a year or so ago I obtained a solution here to be able to batch replace text in files.

The solution was given by "ahoffmann" as

#!/bin/sh
old=oldtext
new=newtext
find . -type f -exec perl -i.old -pe 's/'"'"$oldtext"'"'/$newtext/' {} \;
exit

That used to work. With newer redhat versions it does not work anymore.
I get line and lines of the following error messages and the text is NOT replaced in the files.
Anyone have an idea what causes this?


Malformed UTF-8 character (unexpected end of string) at -e line 1, <> line 5.
Malformed UTF-8 character (unexpected end of string) at -e line 1, <> line 5.
Malformed UTF-8 character (unexpected end of string) at -e line 1, <> line 5.
LVL 1
dryzoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LuxanaCommented:
0
dryzoneAuthor Commented:
SuSE8.2]$ rpm -q perl
perl-5.8.0-55
SuSE8.2]$

Where the bugfix is in
perl-5.8.3-10

You are right seems to be a bug.
I will try tomorrow and come back, but I am surre this is it!


0
dryzoneAuthor Commented:
Ok, I upgraded Perl (was a real pain) and the error message disappeared. For that I will split points to you if someone else can explain why the script as presented in my problem description still does not work.

#!/bin/sh
old=oldtext
new=newtext
find . -type f -exec perl -i.old -pe 's/'"'"$oldtext"'"'/$newtext/' {} \;
exit

REFUSES to change contents of the files with text from old to new.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

stefan73Commented:
Hi dryzone,
Check if your regular expressions for old text and new text are OK. The Perl call looks fine.

Cheers!

Stefan
0
dryzoneAuthor Commented:
Refuses to do.
I created a simple file with only "oldtext"  in it and it did not replace it.
Anyone can check this example?, thanks.

0
LuxanaCommented:
This is working instead "/mydirectory" enter your absolute path to directory where you have your files do not put this script inside "/mydirectory" because script will change inself as well:

------------------

#!/bin/sh
old=oldtext
new=newtext
find /mydirectory -type f -exec perl -i.old -pe s/$old/$new/ {} \;
exit

----------------------




0
dryzoneAuthor Commented:
Luxana, it did not work. Script exits with no error, but also do not update the file contents. I will remember to split the points for your correct pointer toward the Perl bug report.

I have to up the stakes and increased the points as my sysadmin is useless if I cannot do batch perl commands on numerous files. It will take me weeks to edit all the files by hand.

The remaining issue is why the command does not work anymore.

0
LuxanaCommented:
replacing string "oldtext" with "newtext" string for all files in /tmp/test directory. I tested it and it is working for me. The previous example is working as well. In case you having troubles again paste here real content of file you want to edit and string what is needed to replace.

1) Command example:

# find /tmp/test -type f -exec perl -i.old -pe s/oldtext/newtext/ {} \;

2) Shell script example:

#!/bin/sh
for f in $(find /tmp/test -type f)
do
    cat $f | sed -e 's/oldtext/newtext/g' > tmp$$  \
        && chown --reference=$f tmp$$ 2>/dev/null ; \
            chmod --reference=$f tmp$$ 2>/dev/null ; \
                mv -f tmp$$ $f
done
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dryzoneAuthor Commented:
easy

create a file with only the text

"johnny@bogus.com"



Then run the script to change the contents of the file it to
Jack@notsobogus.com

0
LuxanaCommented:
Hi,

I have file called foo.bar which contains "johnny@bogus.com" (no quotations marks !! ) and this file is stored in /tmp directory.

now I want to replace string "johnny@bogus.com" (no quotations marks !! ) with "Jack@notsobogus.com" (again no quotation marks) inside file foo.bar

here is all procedure:
# cd /tmp/
# pwd
/tmp
# echo "johnny@bogus.com" > bar.foo
# cat bar.foo
johnny@bogus.com
# find /tmp/ -type f -exec perl -i.old -pe 's/johnny\@bogus.com/Jack\@notsobogus.com/' {} \;
# cat bar.foo
Jack@notsobogus.com

----------------------------------------

./lubo
0
dryzoneAuthor Commented:
I have now tried on machines from RH7.3 to Fedora2
Does not work at all.
0
dryzoneAuthor Commented:
Let's take another explicit example.
Is there any non-perl solution???

Let's say a file contains the following entry
OBJECT  = 'dark001'

I then want to change it to:
OBJECT  = 'dark002'
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.