LinuxQuestions.org
Help answer threads with 0 replies.
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, 08:58 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
substitution in perl


hi all.......

This is my script:

local $/ = undef; #slurp mode
$slurp1 = <changed2>;
($lines1) = ($slurp1 =~ /(\s+CURSOR+\s+(\S+)\s+)/s);
if ($lines1){
($slurp1) =~ s/($lines1)//;
}

here i have entire file contents in $slurp1 and $lines1 contains a set of lines..I have tested that...i want to replace contents of $lines with white space...whats wrong with this code...


($slurp1) =~ s/($lines1)/\s+/; i have tried this one also...

can anyone suggest me please.....

Thanks in advance......

Last edited by vinaytp; 06-22-2009 at 09:02 AM.
 
Old 06-22-2009, 09:04 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
have you solved your previous Perl problem that is related to this?
 
Old 06-22-2009, 09:09 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
What exactly the requirement is?
Do you want an inplace replacement, coz slurping a file content is not a good idea.

Last edited by PMP; 06-22-2009 at 09:13 AM.
 
Old 06-22-2009, 09:29 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Try
$slurp1 =~ s/$lines1/ /g;
 
Old 06-22-2009, 09:32 AM   #5
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by ghostdog74 View Post
have you solved your previous Perl problem that is related to this?
ya i solved all my previous problems......
 
Old 06-22-2009, 09:35 AM   #6
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by PMP View Post
Try
$slurp1 =~ s/$lines1/ /g;
thanks for ur reply...but this is not working
 
Old 06-22-2009, 09:36 AM   #7
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by PMP View Post
What exactly the requirement is?
Do you want an inplace replacement, coz slurping a file content is not a good idea.
here i want to replace the contents of $lines1 to white space...i mean i want to delete those lines in $lines1....
 
Old 06-22-2009, 09:48 AM   #8
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Check this

cat test.txt
America
Boston
Fairfield
Japan
Brasil

## Perl Code

{
$/ = 1 ;
my $line = 'Japan';

open (FH, "test.txt");
my $slurp = <FH>;
close(FH);


$slurp =~ s/$line/ /g;

print $slurp;
}


OutPut

America
Boston
Fairfield

Brasil
 
Old 06-22-2009, 10:25 AM   #9
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
your code works for single line selection but the actual problem is this ...please just have a look at this modified code


text.txt contains:
America
Japan
Fairfield
Boston


#!/usr/bin/perl
{
$/ = 1 ;


open (FH, "text.txt");

my $slurp = <FH>;
($line) = ($slurp =~ /(America+\s+Japan)/);
close(FH);

print ($line);
$slurp =~ s/($line)/ /g;
print ($slurp);
}

expected output:
America
Japan


Fairfield
Boston


output obtained:
America
Japan

America
Japan
Fairfield
Boston

This is because $line contains two lines...s/$lines/ /g is not doing for multiple lines...hope u understood my problem....please help me......

Last edited by vinaytp; 06-22-2009 at 10:28 AM.
 
Old 06-23-2009, 12:12 AM   #10
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
I am struggling with this since yesterday...can anyone please help me.......
 
Old 06-23-2009, 01:24 AM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
man perlretut
/m modifier
/s modifier
 
  


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
substitution question in perl sharky Programming 4 11-03-2008 08:10 PM
substitution.... kadhan Programming 12 01-28-2008 11:09 PM
perl regex substitution lrt Programming 3 12-08-2007 08:55 AM
Perl substitution with arrays cramer Programming 2 08-15-2006 12:07 AM
Substitution in Perl rigel_kent Programming 4 06-02-2006 11:11 AM


All times are GMT -5. The time now is 08:03 AM.

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