Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl Question

Options
  • 06-07-2009 4:05pm
    #1
    Registered Users Posts: 465 ✭✭


    Hi

    File I/O question, I can use the following code to open my output file, open an input file and write to the ouput file but how do i cut it from the input file permently and write it to the output file.

    # open the output file
    open (OUTPUT, ">$outputFile")or die "Cannot open output file: $!";

    # open the input file and look for the matching text
    open(IN, $input) or print LOGFILE scalar(localtime)." Cannot open input file: $!\n" and exit 1;

    while (<IN>) {

    if ($_ =~ m/applicant/) {
    chomp;
    print OUTPUT $_;
    }
    }
    close OUTPUT;
    close IN;

    thanks.


Comments

  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    I am not quite clear what you mean by "cut it from the file permanently".

    If you want to delete the entire input file you can use the unlink function.

    If you would like a new input file without the lines that you wrote to the output file, you can open a 3rd file and write out the lines that don't have applicant in them (an 'else' for your m// test).
    Then, when you "close IN;" you can rename( $thirdfile, $input ).


  • Registered Users Posts: 465 ✭✭coco06


    sorry about the lack of clarity, what i want to achieve is to match a string, take it and the line following it and write it out to a second file. But i want to remove it from the input file at the same time, so the input file looks something like this

    name: John Doe
    23
    name: Jane Doe
    37
    name: Joe Doe
    12

    so when it matches 'name:' i want it to take the whole line and the one that follows it, ie the value and then stop when it doesnt match any more instances of 'name' so it will take all off the above from the input file and write it out to an external file.

    thanks for reply.. something to think about with the 3rd file and i already use the unlink further down in my script when finished processing.


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    coco06 wrote: »
    sorry about the lack of clarity, what i want to achieve is to match a string, take it and the line following it and write it out to a second file.
    In this case, when you match 'name' you will need to read the input file again and write the second line to the output file.
    if ($_ =~ m/name:/) {
      print OUTPUT $_;
      <IN>;  # Read in following line
      print OUTPUT $_;  # and write it.
    }
    


  • Registered Users Posts: 66 ✭✭bala


    coco06 wrote: »
    sorry about the lack of clarity, what i want to achieve is to match a string, take it and the line following it and write it out to a second file. But i want to remove it from the input file at the same time, so the input file looks something like this

    name: John Doe
    23
    name: Jane Doe
    37
    name: Joe Doe
    12

    so when it matches 'name:' i want it to take the whole line and the one that follows it, ie the value and then stop when it doesnt match any more instances of 'name' so it will take all off the above from the input file and write it out to an external file.

    thanks for reply.. something to think about with the 3rd file and i already use the unlink further down in my script when finished processing.

    1) open and Read the input file to array
    2) close Input file.
    3) loop through the array and process the array elements
    4) If the pattern matches write to output file if not write to input file
    5) close both files


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    bala wrote: »
    1) open and Read the input file to array
    2) close Input file.
    3) loop through the array and process the array elements
    4) If the pattern matches write to output file if not write to input file
    5) close both files
    This is a simple way to do this. I didn't suggest it in case the input file was very big.


  • Advertisement
  • Registered Users Posts: 66 ✭✭bala


    daymobrew wrote: »
    This is a simple way to do this. I didn't suggest it in case the input file was very big.

    Simpler way if worried about large input files

    open input file
    open 2 output files output1 and output2

    parse input
    on pattern match write to Output2
    else write to output1

    close everything

    in the OS rename output1 to input file


  • Registered Users Posts: 465 ✭✭coco06


    working on it now, thanks for the replies people.


  • Registered Users Posts: 66 ✭✭bala


    coco06 wrote: »
    working on it now, thanks for the replies people.

    If you are adventurous then try the File::Inplace module


  • Closed Accounts Posts: 7 bladez


    Perl stronget point is file access. Used it many time to chew multi gig files.
    Streaming the input to ouput is the trick.

    use File::Copy;

    my $source = "SOURCE_FILE";
    my $dest = "DEST_FILE";
    my $match = "something";

    &strem_process($source,$dest,$match);

    sub stream_process(){
    my ($source,$dest,$match) = @_;
    open SOURECE , $source;
    open DEST1, ">" ,$dest;
    open DEST2, ">" ,$source."_tmp";
    while(<SOURCE>){
    if($_ =~ m/$match/){
    print DEST1 $_;
    }
    else{
    print DEST2 $_;
    }
    }
    close SOURCE;
    close DEST1;
    close DEST2;
    move($source."_tmp",$source);
    }


  • Registered Users Posts: 465 ✭✭coco06


    Thanks for replies, all very helpful.


  • Advertisement
Advertisement