rm a word in bunch of file

I have many files in my directory with the string xxxxxx in all of them.
How can i use perl script to rome this string from all of them.

Thanks
komlaaaAsked:
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.

manav_mathurCommented:
Is it really necessary to use PERL??

This can be done through a simple shell script.

sed 's/run/walk/g' < logfile1.dat > logfile2.dat   will replace all occurences of "run" with "walk" in the file logfile1.dat and store the result in logfile1.dat.

You could then create a wrapper for feeding all files here.

Manav
0
komlaaaAuthor Commented:
it not just one file, i think using perl should be easy.

here my version but not working properly.
============== MY VERSION ==============
#!/usr/local/bin/perl



@Files = glob "*";

foreach $file (@Files)
{
    open(IN, ">$file");
    $string = <IN>;
    ($st = $_) =~ s/xxxxxx$//;
    open(OUT, ">$file");
    print OUT "$st";
    close OUT;
    close IN;
}


0
manav_mathurCommented:
komlaa,

open (IN,">$file") ;

should be

open(IN, "< $file") ;

You have to use a < , not a > 

Manav
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

komlaaaAuthor Commented:
i made the change and i am having error message:

Global symbol "@Files" requires explicit package name at .//replace.pl line 6.
BEGIN not safe after errors--compilation aborted at .//replace.pl line 6.
0
manav_mathurCommented:
@Files = glob "*";
foreach $file (@Files)
{
    open(IN, "<$file");
      open(OUT, ">$file.tmp");
    while (<IN>) {
    ($st = $_) =~ s/xxxx//g;
    print OUT $st;
      }
    close OUT;
    close IN;
    system "mv $file.tmp $file" ;
}


This should work

Manav
0
manav_mathurCommented:
use
my @Files = glob("*") ;

instead of
@Files = glob("*") ;

Manav
0
manav_mathurCommented:
By the way,
small errata in my forsi post

>sed 's/run/walk/g' < logfile1.dat > logfile2.dat   will replace all occurences of "run" with "walk" in the file logfile1.dat and >store the result in logfile1.dat.

The results will be stored in logfile2.dat and not logfile2.dat

Cheers
Manav

0
manav_mathurCommented:
Komlaa,

Do not try updating the same file for input and output. I tried that and it doent work.

This is a safer method. If you dont wanna use system, after the script runs, you can schedule a script that takes care of all the .tmp files. or you can do them yourself. By the way, as long as you are on *NIX, I dont see any harm in the script that I've given you .



Cheers
Manav

0
manav_mathurCommented:
use strict;                           #to prevent variable declaration errors
use warnings;                     #like the one you were getting Global symbol blah blah requires explcit blah blah
my @Files = glob "*";
foreach my $file (@Files)
{
    open(IN, "<$file");
      open(OUT, ">$file.tmp");
    while (<IN>) {
    (my $st = $_) =~ s/xxxxx$//g;
    print OUT $st;
      }
    close OUT;
    close IN;
    system "mv $file.tmp $file" ;
}

is not giving me any errors.

Cheers
Manav
0
komlaaaAuthor Commented:
i tryed your code above but i am still having the same error
0
TintinCommented:
Using a shell script, you'd do:

#!/bin/sh

for file in *
do
   sed 's/xxxx//' $file >/tmp/$$ && mv /tmp/$$ $file
done

or a Perl script (or command line to be precise, you'd do

perl -i.bak -pe 's/xxxx//' *

The -i.bak will create a backup of all files changed.
0
wesly_chenCommented:
Hi,

   For Tintin's script, it may need to be modified as follow:
----------
#!/bin/sh

FILES=`cat filelist.txt`

for file in $FILES
do
   sed 's/xxxx//' $file >/tmp/$$ && mv /tmp/$$ $file
done
---------
Where filelist.txt contains all the files you want to replace strings.

   I like Tintin's script which is clear and neat.

Wesly
   
0
komlaaaAuthor Commented:
why specifically filelist.txt... i have such file, in my dir
0
komlaaaAuthor Commented:
sorry i meant i have no such file called filelist.txt in my dir!! Or it is a key word?
0
wesly_chenCommented:
Hi,

   filelist.txt is used more for the files in different directories. It gives you fexibility.

Wesly
0
wesly_chenCommented:
Hi,

   You need to create the "filelist.txt" if you have files in different directories for string replacement.

Wesly
0
Aqui10Commented:
This will do it : )

perl -pi -e ' s/some/other/ig; ' *
Or in your case  ::

perl -pi -e ' s/xxxxxx//; ' *

Dhruv
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
TintinCommented:
Aqui10.

And your answer differs from my by....?
0
ITcrowCommented:
Perl command line.
perl -i.old -p -e 's/foo/bar/g' *

There are some obvious advantages of using 'xargs'
find . -type f ¦ xargs perl -i.old -p -e 's/foo/bar/g'

Perl elaborative, should be something like: ( Untested Code )
foreach (glob "*") {
   open( SRC, $_ ) || die "$!";
    @lines = <SRC>; close( SRC );
    open( DST, $_ ) || die "$!";
    foreach( @lines ) {
        s/foo/bar/g;
         print DST "$_";
    }
    close( DST );
}
0
nochkinCommented:
Why so complicated? If you have recent enough sed then you can do this one-liner:

sed -i.oldtemp -s "s/word//"  *

That will remove "word" from all files in the current directory.
Then you will have manye *.oldtemp files as backup of your originals. If everything went okay then you can remove all *.oldtemp files.
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
Perl

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.