Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

filehandle regex

by pinnacle (Acolyte)
on Nov 16, 2010 at 19:45 UTC ( #871815=perlquestion: print w/ replies, xml ) Need Help??
pinnacle has asked for the wisdom of the Perl Monks concerning the following question:

Question

I am trying to print line above the the regex in below program in if statement in the file. I am using $` but it does not display any output. I checked my syntax which is correct. Please assist
#!/usr/bin/perl use strict; open(ORA, "/usr/cle_un"); while(<ORA>){ print "test: $`\n" if /^Func_file_permission$/; } close(ORA);
File details
ram: 702 Func: Func_file_permission testing tam: 768 Func: Func_file_permission test cham: 803 Func: Func_file_permission test
I want my results to show:
ram: 702 tam: 768 cham:803

Comment on filehandle regex
Select or Download Code
Re: filehandle regex
by ikegami (Pope) on Nov 16, 2010 at 19:57 UTC

    I am using $` but it does not display any output.

    There's nothing before /^/ in the matched variable, so there's nothing in $`.

    The solution is to stop overwriting the value you want.

    my $prev; while (<>) { print($prev) if /^Func_file_permission$/; $prev = $_; }
Re: filehandle regex
by 7stud (Deacon) on Nov 16, 2010 at 20:13 UTC

    Also learn to open() a file correctly:

    open my $INFILE, '<', 'filename' or die "Couldn't open file: $!"; while (my $line = <$INFILE>) { ...
Re: filehandle regex
by jwkrahn (Monsignor) on Nov 16, 2010 at 20:43 UTC
    $ echo " ram: 702 Func: Func_file_permission testing tam: 768 Func: Func_file_permission test cham: 803 Func: Func_file_permission test " | perl -e' local $/ = ""; while ( <> ) { print /\A(.*\n)/ if /Func_file_permission$/m; } ' ram: 702 tam: 768 cham: 803
Re: filehandle regex
by JavaFan (Canon) on Nov 16, 2010 at 20:59 UTC
    To print the previous line, one way of doing it is:
    my $prev = ""; while (<ORA>) { print $prev if /^Func_file_permission$/; $prev = $_; }

      Thanks for the solution it works!! One thing I don't understand why we are using my $prev = ""; also $prev = $_; due to which I am getting previous line from the file

      What's the magic behind it

      Thanks!!

        Yes, it will also work, but then the script will not run under use strict;.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        The my $prev part is there to properly declare the variable. The = "" is there to not have warnings if the first line actually matches. The ; is there to separate statements.
Re: filehandle regex
by aquarium (Curate) on Nov 16, 2010 at 22:38 UTC
    instead of using the following static text line as an anchor..why not use a regex to match what looks like a pattern...e.g. /^[a-z]+:\s[0-9]+$/
    the hardest line to type correctly is: stty erase ^H
Re: filehandle regex
by CountZero (Bishop) on Nov 17, 2010 at 07:18 UTC
    If you are sure the data you want is always on the first line of each block, you could try this:
    use Modern::Perl; { local $/ = ''; say +(split /\n/)[0] while <DATA>; } __DATA__ ram: 702 Func: Func_file_permission testing tam: 768 Func: Func_file_permission test cham: 803 Func: Func_file_permission test

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-21 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (176 votes), past polls