Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Want to do One line Perl Command

by EchoAngel (Pilgrim)
on Jul 02, 2004 at 20:31 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (None)
    As of 2025-03-21 07:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      When you first encountered Perl, which feature amazed you the most?










      Results (63 votes). Check out past polls.