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
RS
means that awk reads in each block one at a time. When[token]
matches, the flagf
is set. Whenf
is set and the substitution is successful,f
is unset, so no other substitutions are made. The1
at the end is a commonly used shorthand which means that each line is printed. TheORS
is the output record separator, which is set to two newline characters so you don't lose the space between each block.