Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

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
Avatar of manav_mathur
manav_mathur

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
Avatar of komlaaa

ASKER

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;
}


komlaa,

open (IN,">$file") ;

should be

open(IN, "< $file") ;

You have to use a < , not a > 

Manav
Avatar of komlaaa

ASKER

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.
@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
use
my @Files = glob("*") ;

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

Manav
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

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

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
Avatar of komlaaa

ASKER

i tryed your code above but i am still having the same error
Avatar of Tintin
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.
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
   
Avatar of komlaaa

ASKER

why specifically filelist.txt... i have such file, in my dir
Avatar of komlaaa

ASKER

sorry i meant i have no such file called filelist.txt in my dir!! Or it is a key word?
Hi,

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

Wesly
Hi,

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

Wesly
ASKER CERTIFIED SOLUTION
Avatar of Aqui10
Aqui10

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Aqui10.

And your answer differs from my by....?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.