#!/usr/bin/perl use strict; use warnings; my $cmd = "/usr/sbin/ntpq -p"; my @ntpout = grep (/^\*/, `$cmd`); # grep line starting with * sign and put into an array my $file = 'offset.txt'; open (my $fh, '>' $file) or die "Could not open file '$file' $!"; print $fh "@ntpout\n"> close $fh; # Written array output into file my $offset = `cat offset.txt | awk '{print $9}'`; # here expecting 9th cloumn value in variable $offset print "$offset \n";