The following awk script will add an entry SAMPLE to block NOTES. I have multiple blocks with the same name. What needs to be changed in this command to add the entry SAMPLE to first block NOTES only?
a class.txt with following content.
[serverClass:NOTES:new]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29
[serverClass:NOTES]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29
[serverClass:NOTES:new23]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29sdf
after running this command
awk -vRS= -vORS='\n\n' '
BEGIN{z="whitelist.0=SAMPLE";FS="\n"}
/NOTES/{
if (/[0-9]=/){
split($NF, a, /[.=]/);
sub(/0/, a[2]+1, z)
}
sub (/$/,"\n"z ,$0)
};1' class.txt
output:
[serverClass:NOTES:new]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29
whitelist.4=SAMPLE <-------
[serverClass:NOTES]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29
whitelist.4=SAMPLE <-------
[serverClass:NOTES:new23]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29sdf
whitelist.4=SAMPLE <-------
what need to be changed in above awk command to add to the block match [serverClass:NOTES] only?
expected:
[serverClass:NOTES:new]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29
[serverClass:NOTES]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29
whitelist.4=SAMPLE <------- should be
[serverClass:NOTES:new23]
whitelist.0=TEST
whitelist.1=FRIDAY
whitelist.2=SPOON
whitelist.3=GAME29sdf
/NOTES]/instead of/NOTES/.