#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use feature 'say'; open (my $fhIn, '<', "in.txt") or die "Can not open 'in.txt': $!"; open (my $fhOut, '>', "out.txt") or die "Can not open 'out.txt': $!"; my %hash = (); my $count = 0; while (<$fhIn>) { chomp; next if (//); if (/,".$hash{ODBIC}.",".$hash{ODBOC}.""; } else { say $fhOut "Update Command MSISDN=notSet,".$hash{ODBIC}.",".$hash{ODBOC}.""; } delete $hash{MSISDN}; delete $hash{ODBIC}; delete $hash{ODBOC}; delete $hash{CF}; } } close ($fhIn) or warn "Could not close 'in.txt': $!"; close ($fhOut) or warn "Could not close 'out.txt': $!"; __END__ $ perl test.pl $VAR1 = { 'MSISDN' => '431234567893', 'ODBOC' => 'BAOC', 'ODBIC' => 'BAIC', 'CF' => { 'CFNRC' => 'CfnrncNotSet', 'CFB' => 'CfbNotSet', 'CFD' => 'CFD-TS10-ACT-91436903000-YES-YES-25-YES-65535-YES-YES-NO-NO-NO-YES-YES-YES-YES-NO;', 'CFNRY' => 'CfnrnyNotSet', 'CFU' => 'CfuNotSet' } }; $VAR1 = { 'ODBIC' => 'BICCROSSDOMESTIC', 'ODBOC' => 'BAOC', 'MSISDN' => '431234567899', 'CF' => { 'CFNRY' => 'CfnrnyNotSet', 'CFU' => 'CFU-TS10-ACT-914369050045021-YES-NO-NONE-YES-65535-YES-YES-NO-NO-NO-NO-NO-NO-NO-NO', 'CFB' => 'CfbNotSet', 'CFNRC' => 'CfnrncNotSet', 'CFD' => 'CFD-TS10-REG-91436903000-YES-YES-25-YES-65535-YES-YES-NO-NO-NO-YES-YES-YES-YES-NO' } }; $ cat out.txt Update Command <431234567893>,BAIC,BAOC Update Command <431234567899>,BICCROSSDOMESTIC,BAOC