Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Want to do One line Perl Command

by EchoAngel (Pilgrim)
on Jul 02, 2004 at 20:31 UTC ( #371501=perlquestion: print w/ replies, xml ) Need Help??
EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

i have a file with the following
apple.c pears.c watermelons.c
i want to delete these files using some code in perl -ne however if my file contains stuff like
apple.c Errors Errors pears.c Errors Errors Errors watermelon.c
i want the perl command line to delete only the watermelon.c file since it has no Errors statements following the file name in the list I hope I made this clear

Comment on Want to do One line Perl Command
Select or Download Code
Re: Want to do One line Perl Command
by Sidhekin (Priest) on Jul 02, 2004 at 20:43 UTC

    If I read you right ...

    perl -nle '$_ eq "Errors" ? pop(@f) : push(@f, $_); END {unlink @f}'

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

      Oh sorry, I forgot to indicated that there could be multiple Errors statements

        Oh sorry, I forgot to indicated that there could be multiple Errors statements

        Oh well. Slight modification:

        perl -nle '$_ eq "Errors" ? delete($f{$prev}) : $f{$prev=$_}++; END {u +nlink keys %f}'

        print "Just another Perl ${\(trickster and hacker)},"
        The Sidhekin proves Sidhe did it!

Re: Want to do One line Perl Command
by delirium (Chaplain) on Jul 03, 2004 at 12:23 UTC
    If we make an assumption that any line that doesn't have a .c file is an error statement...

    perl -lne '/\.c$/?$h{$l=$_}++:delete$h{$l}}{unlink keys%h' file.txt

Re: Want to do One line Perl Command
by Neutron Jack (Pilgrim) on Jul 03, 2004 at 19:36 UTC
    Here's a different approach:
    perl -0ne 'unlink /.+\n(?:Errors$)+|(.+)/mg'
      Hmm... I just don't see why it's so important to you to have a perl -ne style one-liner. What's wrong with a script which can do the job easier and more readable than a one-liner?
      "2b"||!"2b";$$_="the question"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2014-09-23 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls