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

Hi All, Actually My query is to enter in some path(directory) or folder first.

(i)Then there will be 20 to 30 files in both string & number format file names. I want to access only Number format files (Like 2345.45. 5432.321).

The problem here is there are 20 files with Number format file name, i've to read content in each file, by comparing with string value(which is created dynamically).

(ii)Once the string comparison is matched,in that file, i want to extract a value from EXIT(this will be present in all the file Like, EXIT : 0 or EXIT :1) using Grep Command.

(iii) After all these, i want to update this value(EXIT) in EXCEL or ACCESS with some comments. (OR) If the string comparison is not matched in then (ii) step , then i want update some comments in EXCEL or ACCESS.

(iii) Initially i'm trying this with windows server. If its possible, then i need to access unix server with credentials.

Please Can some one help me , how can i approach for this. As i'm new to perl. Anywayz i learning side by side. Just wanted to knw the approach.

Sorry for using improper format. I guess, atleast now its bit clear.

  • Comment on Access to directory -> Files -> contents & get the data

Replies are listed 'Best First'.
Re: Access to directory -> Files -> contents & get the data
by Anonymous Monk on Sep 18, 2010 at 04:13 UTC

    What have you tried? What version of Perl are you using? How are you defining the value of a "variable" within a text file (eg, is it a particular configuration file format)?

    Here's something that may or may not match your description and requirements (mostly untested code):

    use strict; use warnings; use 5.10; # ~~ operator requires Perl version >= 5.10 use Util::Any -all; use Perl6::Slurp; use File::chdir; use Cwd "abs_path"; say for map { map { (split)[1] } grep { /baz/ } @$_ } # value of "baz" grep { "foo bar" ~~ $_ } # "foo" has the value "bar" map { [slurp] } grep { looks_like_number $_ } map { $CWD = $_ and <*> } apply { abs_path $_ } # dies on error $ARGV[0]

      Sorry, that should have been use 5.10.0;.
Re: Access to directory -> Files -> contents & get the data
by BrimBorium (Friar) on Sep 18, 2010 at 08:44 UTC

    I strongly recommend to read "How do I post a question effectively?" and "How (Not) To Ask A Question", because the Monastery is no "we solve your problem for free" page, it's a "we help you to help yourself" page.

    And that it's ugent for you does not mean it's urgent for us. There is always the option to hire an expert to get the job done. There are several ways to solve it in perl, which one is yours?

    BTW if you format you post for better readibility the motivation to spend time to help you increases. Therfore we have a preview button.

      I apologise for not using proper text format.I'll make it clear now.

      Assume there are 10 files with number-format file names in a directory(Like 1110.9089, 1150.0956 & etc). I need to open each file & check for the particular content in the file.

      (i)If the string para201(assume) is present in one of the file after searching through each file, then using Grep i want to extract the value of a variable EXIT(as it will be 0 or other).

      (ii) If a string para201 is not present in all the 10 files, then i can give die to exit.

      (iii)After getting value of EXIT, i will update that in EXCEL or ACCESS.

      This is the concept. This i need to do it in Unix authenticated server. For intial simple coding purpose, I'm trying in windows.

        Frankly, this sounds like homework. In addition, it doesn't look like Perl is the best tool for this job - or at least not Perl by itself. In fact, it sounds like you're trying to force the job to fit the tool rather than using the right tool for the job.

        Unfortunately, your English isn't quite up to the task of explaining exactly what you want. "i want to extract the value of a variable EXIT(as it will be 0 or other)" - does that mean there's a variable called 'EXIT', or perhaps '$EXIT' in the file, or do you want the exit value of 'grep' - which will be 0 if it finds the line in the file and 1 if it doesn't?

        Just for my own amusement, I'm going to define the problem based on my best guesses and give it a shot. Hopefully, this will give you some ideas that you can use to write your own code.

        count=$(ls [0-9]*|wc -l) # Count filename +s starting with a number found=$(grep -c 'para201' [0-9]*|grep -c '1$') # Count how many + of them contain 'para201' echo $(($found==$count?0:1)) # Return 0 if th +e two are the same, 1 otherwise

        --
        "Language shapes the way we think, and determines what we can think about."
        -- B. L. Whorf

        ok, you re-formatted your text. Good start. You can also edit you previous posts in the same way.

        But I guess you did not read the nodes I suggested. You show some kind of design, but not what you tried for implementation. What is you code and where is the problem you can not solve?

        As cdarke pointed out you should check some documentaion, to get the filenames you coud use opendir readdir or File::Find or if you know the files already just loop over the files, open them and check the content, or ... so much ways ... again: which way did your choose?

A reply falls below the community's threshold of quality. You may see it by logging in.