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

x
?
Solved

rm a word in bunch of file

Posted on 2004-11-01
20
Medium Priority
?
853 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:komlaaa
[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
  • 5
  • 3
  • +4
20 Comments
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12465201
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
 

Author Comment

by:komlaaa
ID: 12465239
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12465271
komlaa,

open (IN,">$file") ;

should be

open(IN, "< $file") ;

You have to use a < , not a > 

Manav
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:komlaaa
ID: 12465328
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12465351
@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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12465362
use
my @Files = glob("*") ;

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

Manav
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12465389
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12465420
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 12465471
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
 

Author Comment

by:komlaaa
ID: 12465657
i tryed your code above but i am still having the same error
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12466527
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
 
LVL 38

Expert Comment

by:wesly_chen
ID: 12468778
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
 

Author Comment

by:komlaaa
ID: 12469130
why specifically filelist.txt... i have such file, in my dir
0
 

Author Comment

by:komlaaa
ID: 12469142
sorry i meant i have no such file called filelist.txt in my dir!! Or it is a key word?
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 12469146
Hi,

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

Wesly
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 12469165
Hi,

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

Wesly
0
 

Accepted Solution

by:
Aqui10 earned 400 total points
ID: 12471830
This will do it : )

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

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

Dhruv
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12478723
Aqui10.

And your answer differs from my by....?
0
 
LVL 5

Assisted Solution

by:ITcrow
ITcrow earned 100 total points
ID: 12479239
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
 
LVL 3

Expert Comment

by:nochkin
ID: 12529455
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

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