Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Simple File Editing Question;

Posted on 2004-10-30
12
Medium Priority
?
222 Views
Last Modified: 2013-12-16
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.
0
Comment
Question by:dryzone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 10

Expert Comment

by:Luxana
ID: 12454181
0
 
LVL 1

Author Comment

by:dryzone
ID: 12455361
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
 
LVL 1

Author Comment

by:dryzone
ID: 12460165
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 12

Expert Comment

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

Cheers!

Stefan
0
 
LVL 1

Author Comment

by:dryzone
ID: 12471904
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
 
LVL 10

Expert Comment

by:Luxana
ID: 12478259
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
 
LVL 1

Author Comment

by:dryzone
ID: 12483345
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
 
LVL 10

Accepted Solution

by:
Luxana earned 500 total points
ID: 12488378
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
 
LVL 1

Author Comment

by:dryzone
ID: 12598251
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
 
LVL 10

Expert Comment

by:Luxana
ID: 12600786
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
 
LVL 1

Author Comment

by:dryzone
ID: 12609342
I have now tried on machines from RH7.3 to Fedora2
Does not work at all.
0
 
LVL 1

Author Comment

by:dryzone
ID: 12609361
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question