in reply to word search in perl script

You are making pairs of API requests. You could be working with inconsistent results. You should be opening a pipe and reading one consistent report. Try replacing lines 24 - 29 with something like: (untested)

open my $awspipe, '-|', qw/aws cloudformation describe-stack-drift­- +detection-status --stack-drift-detect­ion-id/, $ID or die "open aws pipe: $!"; while (<$awspipe>) { chomp; my @f = split /"/; $DETECTION = $f[3] if /DetectionStatus/; $SYNC = $f[3] if /StackDriftStatus/; } close $awspipe or die "close aws pipe: $!";

I translated your grep/awk pipeline because I do not know what your input data looks like, but Perl programmers would normally use regexes with a capture group instead of splitting on " and matching key names blindly. If the aws command outputs JSON, normal practice is to parse the objects using JSON instead of slicing up the text.