Get list of partitions from ini file using shell (sed / awk)

I want to create var from the section names of the ini file, for example:

[foo]
; ...

[bar]
; ...

[baz:bar]
;...

now i need var like

SECTIONS="foo bar baz"

early

+3
source share
3 answers

Now I'm using this construct, I don’t need to know if the section exists. just read it, it is empty, it does not exist: D

INI_FILE=test.ini

function ini_get
{

    eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
        -e 's/;.*$//' \
        -e 's/[[:space:]]*$//' \
        -e 's/^[[:space:]]*//' \
        -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
    < $INI_FILE \
    | sed -n -e "/^\[$1\]/,/^\s*\[/{/^[^;].*\=.*/p;}"

    echo ${!2}
}


IP=$(ini_get 50001 ip)
PORT=$(ini_get 50001 port)
echo $IP:$PORT
0
source

A single line solution can be:

export SECTIONS=`grep "^\[" test.ini |sort -u | xargs | tr '\[' ' ' | tr '\]' ' ' `
+2
source
SECTIONS=$(crudini --get your.ini | sed 's/:.*//')
+1
source

Source: https://habr.com/ru/post/1765052/


All Articles