Thank you for sharing your efforts in form of code in your question. Could you please do let us know more clearly what is the logic of getting the expected output. Honestly its not clear(at least to me )
I'm glad I'm not the only one having difficulties with the OP's specification, even though this one - being the third on the same topic - has way more details than the former ones.
The problem you encounter likely has to do with DOS line terminators (<CR> = ^M = \r = 0x0D) in the input file. This is what I get from your code with a "clean" input:
Checking PL AND SC AMF STATUS:
,,
safAmfNode=PL-13,AdminState=LOCKED(1),OperState=ENABLED(1)
, as opposed to what a "contaminated" input yields:
Checking PL AND SC AMF STATUS:
,,
,OperState=ENABLED(1)nState=LOCKED(1)
Let me add that your approach - although working - has quite some potential for improving. Pls check this aprroach:
awk -F, '
BEGIN {print "Checking PL AND SC AMF STATUS:"
}
{gsub ("\r", "")
}
/^safAmfNode/ {TMP = $1
}
/^AdminState/ &&
!/UNLOCKED/ {getline OP
print TMP, $0, OP
}
' OFS="," file
Checking PL AND SC AMF STATUS:
safAmfNode=PL-13,AdminState=LOCKED(1),OperState=ENABLED(1)