Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Replacing a substring from a string in a file

by pop18 (Novice)
on Dec 11, 2007 at 04:28 UTC ( [id://656305]=perlquestion: print w/replies, xml ) Need Help??

pop18 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Replacing a substring from a string in a file
by vivid (Beadle) on Dec 11, 2007 at 04:52 UTC

    Hi, try this,

    use strict; my $arg = $ARGV[0]; open(IN,"$arg")||die("Can't open the input file for reading"); undef $/; my $str = <IN>; close(IN); $str=~s#\$[^\$]+\$#&LimitReplace($&)#gei; open(OUT,">$arg")||die("Can't open the output file for writing"); print OUT $str; close(OUT); sub LimitReplace{ my ($line)=@_; $line=~s#\\limits##gi; return($line); }

    Vivid

Re: Replacing a substring from a string in a file
by shmem (Chancellor) on Dec 11, 2007 at 15:23 UTC
    pop18,

    please don't delete the original text of your posts after your questions have been answered. Fellow monks who happen to read the answers won't see what the original question was, and thus can't learn from the question / answer play any more. Just leaveyour orignal post (OP) as it is, and insert lines below it marked with "update:"

    Please read the entry How do I change/delete my post? in PerlMonks FAQ.

    Thank you.

    OP text:


    Dear All, I need to replace a substring only from a string in a file. I tried with the following coding. But i got the result by replacing all the specified substring in the whole document. I need the substring to be replaced inside the specified string only.

    I want to replace "\limits" inside "$ a+b=\sum\limit_a^{n-j}$"

    ######### open IN, "abc.xml", or die "$!\n"; open OUT, ">iabc.xml", or die "$!\n"; @txt=<IN>; foreach $xabc (@txt) { $find=~ m/\$([^\$]*)\$/g; $find = "\\limits"; $replace = ""; $find = quotemeta $find; $xabc =~ s/$find/$replace/g; print OUT $xabc; } close($xabc);

    Please Help.

    Thanks
    POP


    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Replacing a substring from a string in a file
by kyle (Abbot) on Dec 11, 2007 at 04:43 UTC

    I might slightly rewrite as follows:

    open my $in, '<', 'abc.xml' or die "Can't read abc.xml: $!"; open my $out, '>', 'iabc.xml' or die "Can't write iabc.xml: $!"; while ( my $line = <$in> ) { $line =~ s/\\limits//g; print $out $line; } close $in or die "close input: $!"; close $out or die "close output: $!";

    I note that the string you say you're looking for ("\limits") is not in the example string you give ("$ a+b=\sum\limit_a^{n-j}$"). I also note that the example string appears to be TeX, so a non-Perl solution might be to redefine the macro.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://656305]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-24 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found