Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

How to delete mutiple line from many pm files at one time

by Priti24 (Novice)
on May 19, 2012 at 07:25 UTC ( #971401=perlquestion: print w/ replies, xml ) Need Help??
Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

suppose i have a folder name 'rough' having 10-15 pm files.i have to delete some common lines from all pm files.like...

my $t0 = [gettimeofday]; my $time = tv_interval ( $t0, [gettimeofday]); my @timeData = localtime(time); $time= join(':', @timeData); if($time =~m#^(.*?)\:(\d+)\:(\d+).*?$#) { $time = $3 . ':' . $2 . ':' . $1; }

how should we do that?

Plz help me out

Comment on How to delete mutiple line from many pm files at one time
Download Code
Re: How to delete mutiple line from many pm files at one time
by Corion (Pope) on May 19, 2012 at 07:33 UTC

    You show code, but you don't explain how the code you have relates to the problem you state. Please help us help you better by explaining in prose what the code you have should do, and what it does, and how the two differ.

    Maybe consider looking at Tie::File to treat a file like an array. Or at File::Inplace or at the $^I special variable.

Re: How to delete mutiple line from many pm files at one time
by johngg (Abbot) on May 19, 2012 at 12:22 UTC

    Do you mean that you want to remove the code lines you show from all of the *.pm files? If so, you could so this with in-place editing and a flip-flop (..) operator. You have to make sure you escape any regular expression meta-characters in the code lines you use to delineate start and end of the section to remove.

    knoppix@Microknoppix:~/perl/Monks$ ls d971401/ File1.pm File2.pm File3.pm File4.pm File5.pm knoppix@Microknoppix:~/perl/Monks$
    knoppix@Microknoppix:~/perl/Monks$ perl -ni.BAK -e ' print unless m{^my \$t0 = \[gettimeofday\];} .. m{^\s+\}};' d971401/*. +pm knoppix@Microknoppix:~/perl/Monks$ ls d971401/* d971401/File1.pm d971401/File3.pm d971401/File5.pm d971401/File1.pm.BAK d971401/File3.pm.BAK d971401/File5.pm.BAK d971401/File2.pm d971401/File4.pm d971401/File2.pm.BAK d971401/File4.pm.BAK knoppix@Microknoppix:~/perl/Monks$

    I hope that I have guessed correctly and that this is helpful.

    Cheers,

    JohnGG

Re: How to delete mutiple line from many pm files at one time
by NetWallah (Abbot) on May 19, 2012 at 16:25 UTC
    Here is a one-liner for this:
    perl -p0777 -i.BAK -e 's/my \$t0 = \[gettimeofday\];.+?\.\s*\$1;\s+}/ +/s;' Your.module.names.here
    This takes advantage of the -0 option to set the input record separator to 0x777 (which is not a valid character, and can not occur in your files), and the "s" option in the regex to span matching a '.' across multiple lines.

    The explanation in this debian admin article is mostly applicable - except they use the regex option 'm' to handle a slightly different use-case.

    If you wanted to match line boundaries, in addition to matching '.' across lines, the 's' and 'm' options can be used together.
    Update: Minor Aesthetic change: -iBAK to -i.BAK

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2014-07-26 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls