How do i replace a word ending with "key" using awk excpet for one word?

echo {mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc} | 
awk 'gsub(/^[grp_key,]|abc,|[key;]$/,"") {print}'

Required output

{grp_key,xyz,aaa,ccc}

Try

echo mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc | awk '/grp_key/ {print;next}; /abc|key/ {next}1' RS=, ORS=,
grp_key,xyz,aaa,ccc

The braces cause brace expansion in bash ; if needed, additional action must be taken.

How the input looks like? Is it CSV as you shown? located where? from file , string or something else?

Hi clx ,
You could say it is a string it is not CSV file.

RudiC
Thanks i will try that , i forgot to add i am using korn shell.

Thanks

Hello 100bees,

Following may help you in same too.

 echo mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc | awk -F, '{for(i=1;i<=NF;i++){if($i ~ /grp_key/){A=A?A OFS $i:$i};if($i !~ /key/){A=A?A OFS $i:$i}}} END{print A}' OFS=,
 

Output will be as follows.

 grp_key,abc,xyz,aaa,ccc
 

Suggestion provided by RudiC is great but it will give at last a comma as ORS=, so above may fix that.

Thanks,
R. Singh

% echo "{mbr_key,grp_key,dep_key,abc,xyz,aaa,ccc}" | perl -pe 's/\w+(?<!grp)_key,|abc,//g'
{grp_key,xyz,aaa,ccc}