Search code examples
shellsedscriptingcut

Parse a string and extract a word delimited by comma and assign value from inside [] brackets


I need help to parse a string and extract a word delimited by comma and assign value from inside [] brackets. The input string is like this:

KEEP_DFB,?(y/n),[y];
DFB_VERSION,?(1.4.2/1.7.6),[1.4.2]:

and expected output is

KEEP_DFB=y
DFB_VERSION=1.4.2

The closest I could achieve using sed is this:

echo 'KEEP_DFB,?(y/n),[y]:' | sed 's/\([^,]*,\).*,\([^,]*\):.*/\1=\2/'

but it does not give result as expected.

I also tried 'cut' but the same result as above. Using IFS is not allowed for changing delimiter. Can you please help?


Solution

  • Your were fairly close:

    $ printf "%s\n" 'DFB_VERSION,?(1.4.2/1.7.6),[1.4.2]:' 'KEEP_DFB,?(y/n),[y]:' |
    > sed 's/\([^,]*\),.*,\[\([^],]*\)][;:].*/\1=\2/'
    DFB_VERSION=1.4.2
    KEEP_DFB=y
    $
    

    The first comma is moved outside the capture. The second capture is preceded by \[ (a literal [ in the data) and followed by a ] (doesn't need a backslash escape because ] is only special when it is part of a character class, though I'd be sorely tempted to add one and it works fine with or without the backslash).

    Sundeep noted that there's a semicolon instead of a colon in one of the data lines, but the example data in the echo has a colon rather than a semicolon (which is why I didn't spot the problem on the first pass; I copied the prototype command). That's trivially handled by using [;:] as a character class instead of a direct :.

    The negated character class excludes ] and commas — though it isn't clear why commas need to be excluded. It means you wouldn't recognize this as valid:

    VERSION_LIST,?(1.2/1.3/1.4/1.7),[1.4,1.7]: