Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Edit line or add to end of [global] section of INI file

52 views
Skip to first unread message

Ken Teague

unread,
Oct 2, 2023, 5:47:56 PM10/2/23
to
I have an smb.conf file that is essentially of INI file format.

I'm trying to construct an awk 1-liner that will:
* check for the existence of the following line.
* If it doesn't exist, add it to the *end* of the [global] section.
* If it does exist, make sure it's as follows, or edit it to be as follows:

<tab>dfree command = /bin/dfree


I found this command that somewhat does what I need, but it's prepending a newline before adding the preferred line, so this:
[global]
...
log file = /var/log/samba/%m.log
guest account = guest

[samba]
path = /mount/samba
...

becomes this:
[global]
...
log file = /var/log/samba/%m.log
guest account = guest

dfree command = /bin/dfree
[samba]
path = /mount/samba
...

I'd like for it to look like this:
[global]
...
log file = /var/log/samba/%m.log
guest account = guest
dfree command = /bin/dfree

[samba]
path = /mount/samba
...

It also appears that it's not looking for and editing the line to match my preference (it's just appending):
[global]
...
log file = /var/log/samba/%m.log
guest account = guest
dfree command = /sbin/dfree

dfree command = /bin/dfree
[samba]
path = /mount/samba
...

This smb.conf file is part of a Linux distribution that is used on a hardware appliance. The contents and arrangement of items may change at any time, which is why I'm looking to do this the way I've described. I'm open to ideas if it can be done in a more efficient and reliable manner. Thanks in advance!

Ken Teague

unread,
Oct 2, 2023, 5:51:17 PM10/2/23
to
Sorry, I forgot to add what "this command" is:
awk -vT="dfree command = /bin/dfree" 'x&&$0~T{x=0}x&&/^ *\[[^]]+\]/{print "\t"T;x=0}/^ *\[global\]/{x++}1' /etc/samba/smb.conf
0 new messages