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

    From Ken Teague@21:1/5 to All on Mon Oct 2 14:47:54 2023
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ken Teague@21:1/5 to Ken Teague on Mon Oct 2 14:51:15 2023
    On Monday, October 2, 2023 at 4:47:56 PM UTC-5, Ken Teague wrote:
    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!


    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)