0

the text in file looks like this:

[homes]
        comment = Home Directories
        path = 
        browseable = 
        writable = yes
        valid users = %S
        valid users = MYDOMAIN\%S

[printers]
        comment = All Printers
        path = /var/spool/samba
        browseable = no
        guest ok = no
        writable = no
        printable = yes

i want output as:

[homes]
        comment = Home Directories
        path = /data
        browseable = yes
        writable = yes
        valid users = %S
        valid users = MYDOMAIN\%S

[printers]
        comment = All Printers
        path = /var/spool/samba
        browseable = no
        guest ok = no
        writable = no
        printable = yes

i am using this command:

sed -i "\#path# s#.*#& /data#" file

but it makes changes to everywhere in file where path is located

can anyone help me with this?

Rishav
  • 1
  • 2
  • 1
    you could always comment under your posts instead of opening new questions for each 1 then 2 and now this with same requirement but different issues! – αғsнιη Sep 19 '17 at 10:46

1 Answers1

3

Be more specific in your regular expression:

sed '/path *= *$/ s#$#/data#' file.ini

The expression /path *= *$/ will match any line with path followed by = (possibly surrounded by spaces), but with nothing other than spaces after that and the end of line. The actual substitution will place /data at the end of the line.

This would work too:

sed "s#path *= *$#& /data#" file.ini

If you need to be more specific with what section of the INI-file you want to modify, then expand the range of the s command:

sed '/\[homes\]/,/\[printers\]/ s#path *= *$#& /dev#' file.ini

This will only apply the substitution to the [homes] section.

Redirect to a new file or use -i in the appropriate way with any of the above solutions.

Kusalananda
  • 333,661