Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

print a file from Bottom to Top (reverse order)

by theravadamonk (Scribe)
on Jun 20, 2018 at 16:23 UTC ( [id://1217017]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Monks, I want to know how to print a file from Bottom to Top (reverse order).

I found this below link with below code

https://www.perlmonks.org/?node_id=361397

open(FILE, "<myfile.txt"); @file = reverse <FILE>; foreach (@file) { #process line }

Given below is My CODE with while loop, I want to go with while loop. How can I print from Bottom to Top ?

How can I insert reverse order in to while loop?

What's the easiest way ? I DO hope U monks will come with ideas...

#!/usr/bin/perl use CGI ':standard'; use strict; use warnings; use CGI::Carp 'fatalsToBrowser'; # use only for testing #my $date = localtime(); $ENV{"PATH"} = "/usr/sbin:/usr/bin:/sbin:/bin"; open FILE, '<', '/var/log/maillog' or die $!; while (<FILE>){ chomp; next unless /\S/; # skip blank lines if (/Passed CLEAN/) { #search string Passed CLEAN print "$_ \n"; # print ALL lines containing Passed CLEAN } } close FILE;

Replies are listed 'Best First'.
Re: print a file from Bottom to Top (reverse order)
by haukex (Archbishop) on Jun 20, 2018 at 16:59 UTC

    See File::ReadBackwards:

    use warnings; use strict; use File::ReadBackwards; my $bw = File::ReadBackwards->new('/var/log/maillog') or die $!; while (defined( my $line = $bw->readline )) { chomp($line); print "<$line>\n"; } $bw->close;
Re: print a file from Bottom to Top (reverse order)
by thanos1983 (Parson) on Jun 20, 2018 at 16:33 UTC
Re: print a file from Bottom to Top (reverse order)
by Laurent_R (Canon) on Jun 20, 2018 at 17:59 UTC
    I want to go with while loop.
    Why? Given what you're doing in your second code sample, you could do it as well on the items of the @file array in your first code sample.

    For example:

    open(FILE, "<myfile.txt"); @file = reverse <FILE>; foreach (@file) { chomp; # --> Note: no need to chomp, i +t just forces you to add the \n again when you print next unless /\S/; # skip blank lines --> Note: this is useless giv +en that you print only line matching the /Passed CLEAN/ pattern if (/Passed CLEAN/) { #search string Passed CLEAN print "$_ \n"; # print ALL lines containing Passed CLEAN } }
    Or, simplifying a bit the syntax:
    open(FILE, "<myfile.txt"); @file = reverse <FILE>; foreach (@file) { print if /Passed CLEAN/; }
    That should work fine unless your file is truly huge and won't fit into memory. If your file is too big, then the File::ReadBackwards is the only simple solution;

      Thank you very much for your code. it works as expected. my file is actually maillog file. It's sometimes huge. So File::ReadBackwards is much better. But, the problem is I am trying to insert below lines in to that code.

      if (/Passed CLEAN/) { #search string Passed CLEAN print "$_ \n"; # print ALL lines containing Passed CLEAN }

      But, still no success. I am learning perl. I am still a novice. I googled how to insert it, But, still not lucky.

        Changing just a couple of small things to haukex's solution above should give you what you need:
        use warnings; use strict; use File::ReadBackwards; my $bw = File::ReadBackwards->new('/var/log/maillog') or die $!; while (defined( my $line = $bw->readline )) { print $line if $line =~ /Passed CLEAN/; } $bw->close;

        Given Laurent_R's comprehensive reply, let's move on to the next question: How do I match one or more arbitrary whitespace characters between the  'Passed' and the  'CLEAN' in the pattern match? Please see the  + quantifier in perlre, and  \s in perlre and Whitespace in perlrecharclass. In general, please see perlretut.


        Give a man a fish:  <%-{-{-{-<

Re: print a file from Bottom to Top (reverse order)
by afoken (Chancellor) on Jun 20, 2018 at 18:49 UTC

    tac

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found