Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting a pattern in all files in a directory

Status
Not open for further replies.

amjadcsu

Programmer
Feb 7, 2006
7
US
hi folks
I have a directory called xml_logs which records real time conversation on IRC servers.
This directory consists of xml files
Each file has a format <!CDATA[chat data]]> in it
I need a script to remove this pattern from each file in directory and
the result should be
<chatdata>

i have tried to use sed but sed works on list on files how to make it work on directory in a bash script. any help
thanks
 
then remove the angle brackets <>:


$line =~ s/<!\[CDATA\[([^\]]*)\]\]>/$1/ig;
 
Something generic for all of you guys out there.

Put the script below into the directory with your xml files
It will handle all files in the current directory and all in its tree.
You can call this script from the command line like this

myscript.pl "extension" "the string i am looking for" "the string I want to change it to"

Just remember to install the module File::Find so you will not have to readdir and all that.

"the string i am looking for" or "the string I want to change it to" can have anything you like.
Two things:
if you want to include inside double quotes then change the outside quotes to single. Like this
myscript.pl 'the string i"m looking for' 'the string I want to "change" it to'

And if you want to include strings like !CDATA then backslash the ! (or else you might get errors about 'event not found' and the like) like this:
[blue]myscript.pl 'xml' "<\!CDATA[chat data]]>" 'chatdata'[/blue]

Code:
#!/usr/bin/perl -w

use strict;
use File::Find;

find(\&wanted, '.'); 

sub wanted {
  if($_ =~ /\.$ARGV[0]$/){
    print "File $_ :...";    
    open (FILE, "$_");
    my $file = $_;
    my @file;
    foreach(<FILE>){
      $_ =~ s/$ARGV[1]/$ARGV[2]/g;
      push (@file,$_);
    }
    close FILE;
    open (FL, ">$file");
    foreach(@file){
      print FL $_;
    }
    undef @file;
    close FL;
    print "....fixed\n";
  }  
}


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top