Using sed:
$ cat script.sed
/^Grp: / { ;# A "Grp: " line
s/// ;# Remove "Grp: "
h ;# Save in hold space
s/^/Reporting /p ;# Insert "Reporting " at start, print
d ;# Delete, start next cycle
}
# Any other line:
G ;# Append the hold space
s/\(.*\)\n\(.*\)/\2,\1/ ;# Swap strings around \n, insert comma
$ sed -f script.sed file
Reporting MG1
MG1,user1
MG1,user2
MG1,user3
Reporting MG2
MG2,user7
MG2,user1
MG2,user9
MG2,user6
MG2,user2
As a "one-liner":
sed -e '/^Grp: /{s///;h;s/^/Reporting /p;d;}' \
-e 'G;s/\(.*\)\n\(.*\)/\2,\1/' file
A similar approach to the above with awk:
awk '/^Grp: / { sub("^Grp: ", ""); group = $0; print "Reporting " $0; next }
{ print group "," $0 }' file
Both the sed and awk variations in this answer (as well as the sh variant at the end below) would cope with spaces in the data, either in the MG strings or in the user strings:
$ cat file
Grp: some group ID
line 1
the other line
$ sed -e '/^Grp: /{s///;h;s/^/Reporting /p;d;}' -e 'G;s/\(.*\)\n\(.*\)/\2,\1/' file
Reporting some group ID
some group ID,line 1
some group ID,the other line
Just as a fun exercise, using /bin/sh:
while IFS= read -r line; do
case $line in
'Grp: '*)
group=${line#Grp: }
printf 'Reporting %s\n' "$group"
;;
*)
printf '%s,%s\n' "$group" "$line"
esac
done
Run with
sh script.sh <file
cn:used in your sed with the sample input you provided?