Search code examples
linuxbashshellposix

how to format a variable from text file in bash?


I have been stuck on this for while , and tried many ways but couldn't make it work.

let's say I have a text file , with content as follow

mark_line
variable1
value1
value2
mark_line
variable2
value3
mark_line
variable3
value4
value5
value6
value7
mark_line
...
...
mark_line

and so on , basically between each mark_line , the first line is variable name , and rest are values to that variable , which could be one line or multiple lines

expecting output to become like

variable1="value1 value2"

variable2="value3"

varaible3="value4 value5 value6 value7"

and so on ...

I figure this will need a while or for loop , but I couldn't work out proper way for it.

Any hint or thought would be very much appreciated.


Solution

  • Going solely by the requirement to generate a bunch of assignment statements (but not necessarily execute said statements), we'll start with our sample data file:

    $ cat mark_line.dat
    mark_line
    variable1
    value1
    value2
    mark_line
    variable2
    value3
    mark_line
    variable3
    value4
    value5
    value6
    value7
    mark_line
    variable4
    value8
    mark_line
    mark_line
    mark_line
    

    Next is a bit of awk code that prints variable/value entries to a single line, generating a new line character (\n) only when we see a mark_line entry:

    $ awk '
    /mark_line/ { printf "\n" ; next }
                { printf $0" "}
    ' mark_line.dat
    
    <blank_line>
    variable1 value1 value2
    variable2 value3
    variable3 value4 value5 value6 value7
    variable4 value8
    <blank_line>
    <blank_line>
    

    We'll then parse out the blank lines and have a while loop add the equal sign and double quotes to our output:

    $ awk '/mark_line/ { printf "\n" ; next } { printf $0" "}' mark_line.dat | \
    egrep -v '^$' | \
    while read -r myvar myvals
    do
        echo "${myvar}=\"${myvals}\""
    done
    
    variable1="value1 value2"
    variable2="value3"
    variable3="value4 value5 value6 value7"
    variable4="value8"
    

    NOTE: This solution assumes the each variable? has at least one value? associated with it otherwise a variable? without at least one value? will generate the output: variable5=""

    From here I assume the OP already has a plan on how to use this output (eg, perhaps use the eval command per @kaylum's comment?).