Add a # on particular pattern in file

Hi all

I am looking a way to add a # symbol from a defined pattern to next blank line or carriage return.

for example I have a file in this format:

[G-MI01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456456
thr_act=70
thr_act_w=80
thr_load=60
thr_load_sgw=60
mforce=OK


[P-VE01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456896
thr_act=30
thr_act_w=180
thr_load=60
thr_load_sgw=90
mforce=OK

[P-BA01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456896
thr_act=30
thr_act_w=180
thr_load=60
thr_load_sgw=90
mforce=OK

I whould comment from a specific pattern as [G-MI01SSR] to next carriage return:

[G-MI01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456456
thr_act=70
thr_act_w=80
thr_load=60
thr_load_sgw=60
mforce=OK


#[P-VE01SSR]
#ping_ip=xxx.xxx.xxx.xxx
#counter_ip=xxx.xxx.xxx.xxx
#thr_up=456896
#thr_act=30
#thr_act_w=180
#thr_load=60
#thr_load_sgw=90
#mforce=OK

[P-BA01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456896
thr_act=30
thr_act_w=180
thr_load=60
thr_load_sgw=90
mforce=OK

is there a way to do this job?

thanks!

Almost certainly . . . if you could unambiguously specify the job to be done. What you say verbatim doesn't match your sample data, and there's usually noy carriage return in *nix text files.

Would this paraphrase of your request express your needs:

"in a file of empty line separated records, comment out one record's lines AFTER the record identified by [G-MI01SSR] patter"?

awk '/\[G-MI01SSR\]/ {a++}; a && ! NF {c=1} ; c && NF {b="#"} ; !NF && b {b=_; c=0} {print b $0}; ' infile

This looks like a config file (or ini file) that can be read by python's configparser module. Here's an attempt to comment out a section using python 3 interpreter.

import configparser

file = 'config.txt'

config = configparser.ConfigParser()
config.read(file)

section = 'P-VE01SSR'

for (k, v) in config[section].items():
    config.remove_option(section, k)
    config.set(section, '#' + k, v)

with open(file, 'w') as configfile:
    config.write(configfile)
 
sed '/^.P-VE01SSR.$/,/^$/ s/./#&/' file

sed -i ... writes back to the file (if your sed supports it).

How about

awk '
L                       {sub  (/^/, "#")
                         gsub (/\n/, "&#")
                         L = 0
                        }
/^\[G-MI01SSR\]/        {L = 1}
1
' RS= ORS="\n\n" file