##
grep -A 3 -B 3 xxx file-B >> file-A
####
$out = qx/grep ... / #note that $out contains new lines
####
$last = qx/tail -1 $fileA/
####
if (($last =~ /\S/) && ($out =~/$last(.*)/)) {
$out = $1
}