If all the blocks have exactly the same format (same field names, in the same order), then you can use awk in “paragraph mode”, and print the desired field number in each block. If there are always spaces around the equal signs and the values never contain spaces, you can rely on whitespace-separated fields.
awk -v RS= -v ORS=',' '{print $3, $6, $9, $12, $15, $18}'
If you can rely on the order and presence of fields but not on the whitespace, then you'll need a bit of parsing to split at the equal signs.
awk -v RS= -F '\n' '{
for (i = 1; i <= NF; i++) {
sub(/[^=]*= */, "", $i);
printf "%s%s", $i, (i==NF ? "\n" : ",");
}
}'
Here's a Perl method:
perl -000 -ne '
$, = ","; $\ = "\n";
@kv = split /\n| *= */;
print @kv[grep {$_%2} 0..$#kv];
'
If the fields in the blocks can come out of order and you always want a specific order as output, you'll need to store the fields and print them out in the right order at the end of each paragraph. In awk, this is easier to do in line mode than in paragraph mode.
awk -v OFS=',' '
match($0, / *= */) {a[substr($0,1,RSTART-1)] = substr($0,RSTART+RLENGTH)}
/^$/ {print a["HLRSN"], a["IMSI"], a["KIVALUE"], a["K4SNO"], a["CARDTYPE"], a["ALG"]; split("", a)}
END {print a["HLRSN"], a["IMSI"], a["KIVALUE"], a["K4SNO"], a["CARDTYPE"], a["ALG"]}
'
This one is a one-liner in Perl.
perl -000 -F'/\n|\s*=\s*/' -ane '%F = @F; $\ = "\n"; $, = ","; print @F{qw(HLRSN IMSI KIVALUE K4SNO CARDTYPE ALG)}'
HLRSN
,IMSI
,KIVALUE
, etc. always present in each input block and in the same order each time?