Merge broken lines

ok will tell u the exact story :

The files will appear in a folder called "processing" in a partular directory. The script should target this folder . I was just considering grep folder as an example. What happens is files appear in this processing folder every minute or so. they get processed and move to a folder called sent. Now if there is an error as u know ( blank line or name appearing on next line ) in any of the files they get stuck in processing folder and do not get to sent folder. The files are something lik this after the processing :

GURD0906
GURD0906trim$$
GURD0906Service

Now the file with size 0 will be GURD0906trim$$ ... but we cant edit this file as this will not contain anything. We need to edit GURD0906 file which has the error. So when the script runs it should find the file with size 0... consider the first 8 characters of that file and edit it to remove the error. This file should be moved back to the a folder called "IN" which is in the same place as the processing directory. all the other files like

GURD0906trim$$
GURD0906Service

and the the backed up original file GURD0906.bak

should be moved to a folder called "failed reports" which is also next to processing folder.

And the most important part is if there is no file with size 0. then nothing should be done and backup file should not be created. otherwise we will have disk space issue.

This is the whole thing.

Try next one. Run it with your particular options.

$ cat script2.pl 
use warnings;
use strict;
use Getopt::Long;
use File::Copy qw( move copy );
use File::Spec;

my ($destdir, $bakdir);

GetOptions(
        q[destdir=s] => \$destdir,
        q[bakdir=s] => \$bakdir,
) or die qq[ERROR reading arguments\n];

die qq[Usage: perl $0 --destdir=<dir> --bakdir=<dir> <files>\n] 
        unless defined $destdir &&
                   defined $bakdir &&
                   -d $destdir &&
                   -d $bakdir;


for my $file ( @ARGV ) {
        next if -s $file;
        my $efile = substr $file, 0, 8;
        do {
                local $^I = q[.bak];
                local @ARGV = $efile;
                my @prev_line;
                while ( <> ) {
                        chomp;

                        if ( /\A\*\*/ .. eof ) {
                                        next if m/\A\s*\Z/;
                                        if ( m/�/ ) {
                                                        if ( @prev_line ) {
                                                                        printf qq[%s\n], pop @prev_line;
                                                        }
                                                        push @prev_line, $_;
                                                        next;
                                        }

                                        printf qq[%s\n], (pop @prev_line || qq[]) . $_;
                                        next;
                        }

                        printf qq[%s\n], $_;
                }

                {
                        my $f = $efile . $^I;
                        move( 
                                $f,
                                File::Spec->catfile( $bakdir, $f ) 
                        ) || warn qq[Couldn't move file $f\n];
                }

                move(
                        $efile,
                        File::Spec->catfile( $destdir, $efile )
                ) || warn qq[Couldn't move file $efile\n];

                move(
                        $file,
                        File::Spec->catfile( $bakdir, $file )
                ) || warn qq[Couldn't move file $file\n];

                {
                        my $f = $efile . qq[Service];
                        move(
                                $f,
                                File::Spec->catfile( $bakdir, $f )
                        ) || warn qq[Couldn't move file $f\n];
                }
        }

}
$ perl ../script2.pl  --destdir=../IN --bakdir=../failed /tmp/grep/*

Thanks a lot :slight_smile: will try and get back to u soon.