Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.

by hemantjsr (Initiate)
on Dec 23, 2014 at 07:34 UTC ( [id://1111148]=perlquestion: print w/replies, xml ) Need Help??

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

Please find below Working Code. It's giving error message "Use of uninitialized value $lines1 in substitution (s///) a"

apart from that it's giving a result like in a file01.txt have now file01.txt, in file02.txt have file02.txt only and so on. Old values are deleted and only filename are comeing file.

where as i needed output like

file01.txt have 11 10,9:10/4947000219 :20140924105028 24

file02.txt have 11 10,9:10/4947000220 :20140924105228 24

file03.txt have 11 10,9:10/4947000221 :20140924105428 24

file04.txt have 11 10,9:10/4947000222 :20140924105628 24

#!/usr/bin/perl use strict; use 5.10.0; # For autodie and regex \K use autodie; use File::Find; use Time::Piece; use Time::Seconds qw/ ONE_MINUTE /; use constant DATE_FORMAT => '%Y%m%d%H%M%S'; my $n; my $directory="/home/e/Doc/AutoMation"; chdir $directory; opendir(DIR, ".") or die "couldn't open $directory: $!\n"; foreach my $file (readdir DIR) { next unless -f $file; open my $in_fh, '<',$file; my @lines = <$in_fh>; close $in_fh; ++$n; $lines[0] =~ s~/4947000219/\K(4947000210+)~$1+$n~e; $lines[1] =~ s{:20140924105028\K(20140924105028+)}{ my $tp = Time::Piece->strptime($1, DATE_FORMAT); ($tp + ONE_MINUTE * 2 * $n)->strftime(DATE_FORMAT); }e; open my $out_fh, '>', $file; print $out_fh @lines; close $out_fh; } closedir DIR;
  • Comment on Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.
  • Download Code

Replies are listed 'Best First'.
Re: Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.
by GrandFather (Saint) on Dec 23, 2014 at 08:20 UTC

    '.' is the current directory and '..' is the parent directory. Neither is a file so you can't open them as a file. You can test for files and skip anything that's not a file:

    for my $fileName (@files) { next if ! -f $fileName; open my $in_fh, '<', $fileName; ... }
    Perl is the programming world's equivalent of English
      Or, to put that into "plain English" perl:
      for my $fileName ( @files ) { next unless -f $fileName; ... }
Re: Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.
by graff (Chancellor) on Dec 23, 2014 at 09:13 UTC
    Apart from what GrandFather said, I think you also want to use "chdir", like this:
    my $directory = "/home/e/Doc/AutoMation"; chdir $directory; opendir( DIR, "." ); for my $file ( readdir DIR ) { next unless -f $file; open( my $in_fh, '<', $file ); … }
    Either that, or else you'll need to include the directory name with the file name when you try to do anything with the file:
    opendir( DIR, $directory ); for my $file ( readdir DIR ) { next unless -f "$directory/$file"; open( my $in_fh, '<', "$directory/$file" ); … }
      Or use the glob operator, rather than opendir and readir, which will remove the . and .. directories from the list and preserve the relative path of the entries. But one still has to check if the entries are really files with the -f operator.

      Please find below Working Code. It's giving error message "Use of uninitialized value $lines1 in substitution (s///) a"

      apart from that it's giving a result like in a file01.txt have now file01.txt, in file02.txt have file02.txt only and so on. Old values are deleted and only filename are comeing file.

      where as i needed output like

      file01.txt have 11 10,9:10/4947000219 :20140924105028 24

      file02.txt have 11 10,9:10/4947000220 :20140924105228 24

      file03.txt have 11 10,9:10/4947000221 :20140924105428 24

      file04.txt have 11 10,9:10/4947000222 :20140924105628 24

      #!/usr/bin/perl use strict; use 5.10.0; # For autodie and regex \K use autodie; use File::Find; use Time::Piece; use Time::Seconds qw/ ONE_MINUTE /; use constant DATE_FORMAT => '%Y%m%d%H%M%S'; my $n; my $directory="/home/e/Doc/AutoMation"; chdir $directory; opendir(DIR, ".") or die "couldn't open $directory: $!\n"; foreach my $file (readdir DIR) { next unless -f $file; open my $in_fh, '<',$file; my @lines = $file; close $in_fh; ++$n; $lines[0] =~ s~/4947000219/\K(4947000210+)~$1+$n~e; $lines[1] =~ s{:20140924105028\K(20140924105028+)}{ my $tp = Time::Piece->strptime($1, DATE_FORMAT); ($tp + ONE_MINUTE * 2 * $n)->strftime(DATE_FORMAT); }e; open my $out_fh, '>', $file; print $out_fh @lines; close $out_fh; } closedir DIR;
        What did you expect to happen as a result of these lines?
        open my $in_fh, '<',$file; my @lines = $file; close $in_fh;
        If you expected that the contents of the data file would be read into the @lines array, the second line should be:
        my @lines = <$in_fh>;
        The way you posted it, you're just assigning the file name (value of $file) to be the first element of @lines, and nothing else is placed into the array. That's why you got "use of uninitialized value" when you tried to do something with the second element of the array.
Re: Have a multiple file in directory and want to manipulate in each files in incremental order. All the file have same value.
by GotToBTru (Prior) on Dec 23, 2014 at 15:46 UTC

    This code will not compile, it appears to be missing significant sections, ie, the beginnings of the two blocks you are ending with unmatched }. Please post working code.

    my @lines = $file

    will store the file name in the array. I suspect you actually want to read the lines out of the file into @lines.

    foreach my $file (readdir DIR) { next unless -f $file; open my $in_fh, '<', $file; my @lines = <$in_fh>; close $in_fh; # call subroutine here }

    You will probably want to call a subroutine where indicated to process @lines and write your output file, since your array will cease to exist once the foreach loop is finished.

    Updated per below

    1 Peter 4:10
      It should read
      foreach my $file (readdir DIR)
      instead of
      foreach my @file (readdir DIR)
      Note the $ instead of the @.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-16 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found