Modifying ini files using shell script
Asked Answered
G

3

10

I have an ini file similar to this

[test]
foo=bar

and if we call this ini file as test1.ini

How do I change the value of foo to foobarbaz for example using shell script.

I have tried the following and it doesn't work for me. I don't see the updated changes in the ini file. how do I write it?

sed "/^foo=/s/=.*/=foobarbaz/" < test1.ini

Do you have any other suggestions

Giaour answered 24/10, 2013 at 13:25 Comment(2)
Assuming you mean you want foo=foobarbaz and not foobarbaz=bar, your code worked for me under Windows, mingw (MSYS), bash. Quoting is shell-dependent; try using single quotes (') around the sed string rather than double-quotes (").Isherwood
Use the -i option. sed -i "/^foo=/s/=.*/=foobarbaz/" test1.ini (note that the redirection < has been removed from the command).Angie
F
9

To have the file updated, use the -i option of sed:

sed -i "/^foo=/s/=.*/=foobarbaz/" test1.ini

From man sed:

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if SUFFIX supplied)

So you can also do

sed -i.bak "/^foo=/s/=.*/=foobarbaz/" test1.ini
Final answered 24/10, 2013 at 13:32 Comment(0)
G
12

I personally use a more elaborated sed command, as the same option might appear in several different sections:

sh$ sed -i.bak '/^\[test]/,/^\[/{s/^foo[[:space:]]*=.*/foo = foobarbaz/}' test1.ini
#       ^^^^^^  ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#    make a       in the right         perform the substitution
#   *backup*       section                as you want

And as a safety net, I would add:

sh$ diff test1.ini{,.bak}
2c2
< foo = foobarbaz
---
> foo=bar
Girl answered 9/2, 2015 at 12:23 Comment(0)
F
9

To have the file updated, use the -i option of sed:

sed -i "/^foo=/s/=.*/=foobarbaz/" test1.ini

From man sed:

-i[SUFFIX], --in-place[=SUFFIX]

edit files in place (makes backup if SUFFIX supplied)

So you can also do

sed -i.bak "/^foo=/s/=.*/=foobarbaz/" test1.ini
Final answered 24/10, 2013 at 13:32 Comment(0)
L
4

Crudini

Crudini is a command line interface to .ini files.

Solution

crudini --set test1.ini test foo foobarbaz

Or, more generally,

crudini --set   FILENAME.INI   SECTION   PARAMETER   VALUE

Details

Written in 2013 by Pádraig Brady, crudini has become a standard solution in the UNIX toolkit in the years since this question was first asked. Many GNU/Linux distributions prepackage crudini as part of the OS, making installation simple (e.g., apt install crudini). It also works on Apple MacOS and Microsoft Windows.

I have used my own custom sed scripts in the past, but crudini is easier, more powerful, and less likely to accidentally destroy my files.

See the crudini homepage for more details.

Dangers

While crudini is safer than sed for most people, it is not perfect.

  • Crudini does not have sed's -i~ option to automatically create a backup file when operating in-place.
  • It is way too easy to delete an entire section.
  • crudini --set config.ini foo x=3 will blithely create a line that says, x=3 = . [Reportedly this is fixed in the latest release.]
  • Its handling of lists (comma separated values) is naive.
Larena answered 22/9, 2022 at 3:37 Comment(3)
Thanks for the comments @hackerb9. The third "x=3" issue above is addressed in latest git. Re backups, I was thinking that using external tools like cp to manage backups is more flexible, and more in the unix mode of do one thing wellNorthwesterly
Thanks, @pixelbeat. By editing in-place, crudini is already doing two things. It would be more flexible if crudini honored sed's -i[SUFFIX], --in-place=[SUFFIX] option. That way a person could use cp if they like or request a backup with a specific suffix.Larena
By the way, the traditional Unix folks don't use cp before using a filter on a file, they do this: mv foo foo~; sed 's/fred/juki/' foo~ > foo. That keeps the original file's inode unchanged and reads the file only once. It may make sense for Crudini to support that mode by only doing in-place editing by default when stdout is a tty (the screen).Larena

© 2022 - 2024 — McMap. All rights reserved.