Solved

Simple File Editing Question;

Posted on 2004-10-30
216 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
Question by:dryzone
    12 Comments
     
    LVL 10

    Expert Comment

    by:Luxana
    0
     
    LVL 1

    Author Comment

    by:dryzone
    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
    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
     
    LVL 12

    Expert Comment

    by:stefan73
    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
    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
    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
    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:
    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
    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
    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
    I have now tried on machines from RH7.3 to Fedora2
    Does not work at all.
    0
     
    LVL 1

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
    Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
    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…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now