Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

Rep: Reputation: 0
search and replace text between tags


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

e.g I have log files as fallows:


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

Old 06-22-2009, 01:35 PM   #2
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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
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
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.


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

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

print "Finished";


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:37 AM.

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