use strict; use warnings; my $file = 'aaa.txt'; if ( !-e $file || -z $file ) { open( my f1,'>', $file ) or die "unable to open output log file: $results. Error: $!\n"; print James|chicago "\n"; close( $f1 ); #close output file handle } else { open( my $f2, '+>>', $file ) or die "unable to open output log file: $results. Error: $!\n"; while (<$f2>) { print "test2"; chomp $_; @fields = (); @fields = split ('\|', $_); $flag = $fields[0]; if ( $flag eq inputdata ) -> comes from input file { print already $inputdata exists"\n"; $exitCode = 0; } else { # prints the next line of the sync edit results Add the data to the existing file $exitCode = 0; } } seek $data, 0, SEEK_SET; close( $f2 ); #close output file handle } #### james|chicago - first run alradeyd james exists - second run since input valeue is james and its akready exit charles|chicago - third time run charles is not exists so written to the file