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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Aqui10Connect With a Mentor Commented:
This will do it : )

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

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

Dhruv
0
 
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
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
manav_mathurCommented:
komlaa,

open (IN,">$file") ;

should be

open(IN, "< $file") ;

You have to use a < , not a > 

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

And your answer differs from my by....?
0
 
ITcrowConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.