Linked Questions

Popular Questions

How to add a line to a stanza if it doesn't exist already

Asked by At

I have a file that looks like this:

[TEST]
Path1 = db/test/testdb
Path2 = db/real/db

[TEST2]
Path1 = db/test/testdb
Path2 = db/real/db
DataSizeMB = 1234

Is there any way where I can grep each stanza to see if DataSizeMB = 1234 is there? And if it's not, adding it to the stanza?

What I've tried:

cat test.txt | awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/DataSizeMB/'

which grabs all the stanzas with DataSizeMB. I need to grep all stanzas without DataSizeMB and add the line.

An example output:

[TEST]
Path1 = db/test/testdb
Path2 = db/real/db
DataSizeMB = 1234

[TEST2]
Path1 = db/test/testdb
Path2 = db/real/db
DataSizeMB = 1234

Related Questions