http://www.perlmonks.org?node_id=1082503

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

Hi There, I got a log file which is as follow.
###################################################################
Name -> Name of the person,Path -> include \\path1\\path2\file.txt
#user: machinename\name
#log forward
#Date setup: 12/01/2010
# can be n number of line
Name -> Name of the person
###################################################################
writing a perl script which accept file name as input and match file name with the path and if matches I need to print the name in the last. log file will have n number of block like that and file name can match n number of time. how to proceed. Don';t like to use $flag, looking for best option. my piece of code looks like $file_name is command line parameter. while (<DATA_FILE>) { $str = $_ ; if ($str =~ /include/) { if ($str =~ /\b$file_name\b/) { $flag = "TRUE" ; } } if ($flag eq "TRUE") { if ($str =~ /View Name/) { print $str ; ($junk,$view_name) = split(\->\,$str) ; print $view_name ; $flag = "FALSE" ; } } #print "$_\n"; }

Replies are listed 'Best First'.
Re: Help required in find command.
by InfiniteSilence (Curate) on Apr 16, 2014 at 13:42 UTC

    You should proceed by,

    • Learning how to open and read the contents of a file in Perl
    • Parse out contents with relatively simple regular expressions
    • Close a file handle
    • Use the Perl print statement

    Finally, you might do well to realize that sites like Perlmonks are not here to do your work for you but rather exist to help you with specific problems you encounter while trying to solve your own coding problems.

    Celebrate Intellectual Diversity

Re: Help required in find command.
by robby_dobby (Hermit) on Apr 16, 2014 at 13:40 UTC
    Hello maestromani,

    This post is not meant to be in Perl Monks Discussion, but in Seekers of Perl Wisdom. Please post a question there or wait for one of the moderators to move this to SoPW.

    You have written enough posts to know that you should atleast give some code to show that you've worked out something, but are stuck. Can you show us some code? Programming (and your task) is mostly about teaching the computer what to do - start by enumerating the steps you'd take when executing this task yourself (with primitive abilities that a computer has and not the human sophistication that you possess). Write this all down in your native tongue and the next step is an SMOP :-) Good luck!

Re: Help required in find command.
by CountZero (Bishop) on Apr 16, 2014 at 13:43 UTC
    Please give us a few more blocks, exactly as they appear in your file (you can change names and other sensitive info if required) and put this information in <code> ... </code> tags.

    It is very important to see the exact structure, delimiter, number of lines, ...

    Also give us an example of the output you expect from the data you provide.

    Update: changed <quote> to <code> (thanks choroba)

    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

    My blog: Imperial Deltronics
      Don';t like to use $flag, looking for best option. my piece of code looks like $file_name is command line parameter.
      while (<DATA_FILE>) { $str = $_ ; if ($str =~ /include/) { if ($str =~ /\b$file_name\b/) { $flag = "TRUE" ; } } if ($flag eq "TRUE") { if ($str =~ /View Name/) { print $str ; ($junk,$view_name) = split(\->\,$str) ; print $view_name ; $flag = "FALSE" ; } } #print "$_\n"; }

        Your OP is virtually unreadable so I don't actually know what your problem is.

        The only place in the entire thread that the string 'View Name' appears is in your regex (i.e. '/View Name/'). Therefore, $str =~ /View Name/ will always be FALSE and that if block will never be entered. Perhaps that relates to whatever your problem is. Have you shown a representative sample of your data?

        From the code fragment you've shown here, you're using package global variables exclusively. Perhaps that relates to your problem. For instance, there could be another global $flag elsewhere in your code.

        -- Ken

Re: Help required in find command. (read parse file tokenize m//gc)
by Anonymous Monk on Apr 16, 2014 at 20:58 UTC
    If line oriented, read line by line, if spans multiple lines, slurp, then tokenize, then iterate
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; open my($DATA), '<', \q{ Name -> Name of the person,Path -> include \\path1\\path2\file.txt #user: machinename\name #log forward #Date setup: 12/01/2010 # can be n number of line Name -> Name of the person }; my @tokens; while(<$DATA>){ m{^\s*$}mgcsx and do { push @tokens, ['BLANK', $_ ]; next; }; m{^([#].*)}mgcsx and do { push @tokens, ['COMMENT', $1 ]; next; }; m{\QName -> \E([^,]+)}mgcsx and do { push @tokens, ['NAME', $1 ]; }; m{\QPath -> include \E([^\r\n]+)}mgcsx and do { push @tokens, ['PATH_INCLUDE', $1 ]; }; } dd(\@tokens); __END__ [ ["BLANK", "\n"], ["NAME", "Name of the person"], ["PATH_INCLUDE", "\\\\path1\\\\path2\\file.txt"], ["COMMENT", "#user: machinename\\name\n"], ["COMMENT", "#log forward\n"], ["COMMENT", "#Date setup: 12/01/2010\n"], ["COMMENT", "# can be n number of line\n"], ["NAME", "Name of the person\n"], ]
Re: Help required in find command.
by mhearse (Chaplain) on Apr 17, 2014 at 00:04 UTC
Re: Help required in find command.
by DrHyde (Prior) on Apr 17, 2014 at 10:14 UTC
    I recommend hiring a programmer.