I need to edit the provider line from [token] section in the following file:
$ cat test.txt
[test]
#provider = keystone.token.providers.uuid.Provider
#driver = keystone.token.persistence.backends.sql.Token
#caching = true
#cache_time = <None>
[token]
#provider = keystone.token.providers.uuid.Provider
#driver = keystone.token.persistence.backends.sql.Token
#caching = true
#cache_time = <None>
[trust]
#provider = keystone.token.providers.uuid.Provider
#driver = keystone.token.persistence.backends.sql.Token
#caching = true
#cache_time = <None>
I tried:
sed -e '/^\[token\]/s/^\#provider.*/provider = keystone.token.providers.uuid.Provider/' test.txt
but there were no changes made to the file.
How can I change my script to edit the right section?
Here's how you could do it using awk:
Unsetting the record separator
RSmeans that awk reads in each block one at a time. When[token]matches, the flagfis set. Whenfis set and the substitution is successful,fis unset, so no other substitutions are made. The1at the end is a commonly used shorthand which means that each line is printed. TheORSis the output record separator, which is set to two newline characters so you don't lose the space between each block.