I have a text file name class.txt with follow contents.[serverClass:MAIL]whitelist.0=LATEwhitelist.1=ONTIME[serverClass:LETTER]whitelist.0=FIRSTwhitelist.1=SECONDwhitelist.2=THIRDwhitelist.3=FOURTH[serverClass:NOTES]whitelist.0=TESTwhitelist.1=CARwhitelist.2=SPOONwhitelist.3=GAMELet's say I want to add a new line to one of these blocks e.g. a new entry SAMPLE in block LETTER so whitelist number should auto increment when the new entry is added. Desired output [serverClass:MAIL]whitelist.0=LATEwhitelist.1=ONTIME[serverClass:LETTER]whitelist.0=FIRSTwhitelist.1=OLDwhitelist.2=NEWwhitelist.3=FOURTHwhitelist.4=SAMPLE[serverClass:NOTES]whitelist.0=TESTwhitelist.1=CARwhitelist.2=SPOONwhitelist.3=GAMEis there a way to do this with sed ?
Append line to a block of text incrementing the value from previous line
text processing;sed;awk
As I said in my comment on Ralph's answer, there are better tools for this job e.g. with awk you could use paragraph mode, append whitelist.0=SAMPLE if the block is empty otherwise extract the no. from the last field (in this case the field is a line) and append whitelist.NR+1=SAMPLE to that block:awk -vRS= -vORS='\n\n' 'BEGIN{z=whitelist.0=SAMPLE;FS=\n}/LETTER/{if (/[0-9]=/){split($NF, a, /[.=]/);sub(/0/, a[2]+1, z)}sub (/$/,\nz ,$0)};1' infile