LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-22-2009, 12:58 PM   #1
pchoudhary
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Rep: Reputation: 0
search and replace text between tags


Hi,

I want to know how can I search and replace text between tags using perl

e.g I have log files as fallows:

<valCode>ABCDEF009E76</valCode>
LOTS_OF_OTHER_LINES
<pCode>XYZYFE1009E76</pCode>

I want to remove the text between tags or replace the text with word 'Removed' as fallows

<valCode></valCode>
LOTS_OF_OTHER_LINES
<pCode>Removed</pCode
 
Old 06-22-2009, 01:35 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Yes, you can do that.

If you want help in doing so, you should post what you've done and explain why you're asking: Since perl would not be my first choice of language for this problem - sed or awk seem (to me) to be better candidates - the restriction to a perl solution makes me wonder if this is a homework problem...
 
Old 06-22-2009, 02:12 PM   #3
luisduenas
LQ Newbie
 
Registered: Dec 2008
Distribution: Debian - pero ya no.
Posts: 22

Rep: Reputation: 0
Use xmlerl module.
 
Old 06-22-2009, 02:14 PM   #4
pchoudhary
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Original Poster
Rep: Reputation: 0
I tried the following code is not same always aslo the tag is not considitent in the log files. in log logfiles the developer has used "pcode" in some they have used "Code"

I want to find the tag in the line <pcode> then the end tag </pcode> and then removed or replace the text between the tags.

#!/usr/bin/perl

use strict;
use warnings;
use File::Find;

my $startdir = '/path/to/start/dir';
my $find = 'this';
my $replace = 'that';
my $doctype = 'txt';

print qq~Finding "$find" and replacing it with "$replace"\n~;

find(
sub{
return unless (/\.$doctype$/i);
local @ARGV = $_;
local $^I = '.bac';
while( <> ){
if( s/$find/$replace/ig ) {
print;
}
else {
print;
}
}
}, $startdir);

print "Finished";
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Muilti-line text search/replace Mleahy Linux - Software 8 07-15-2008 09:58 AM
Awk- search & replace text viceroy Linux - Newbie 7 07-22-2007 11:18 AM
Need command to search and replace text in file acascianelli AIX 12 04-11-2007 09:16 PM
another question about search and replace text graziano1968 Linux - General 3 08-02-2006 10:35 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 07:47 PM


All times are GMT -5. The time now is 08:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration