Parse out key=value pairs into variables

I have a bunch of different kinds of files I need to look at periodically, and what they have in common is that the lines have a bunch of key=value type strings. So something like:

Version=2 Len=17 Hello Var=Howdy Other

I would like to be able to reference the names directly from awk... so something like:

cat some_file | ... | awk '{print Var, $5}' # prints Howdy Other

How can I go about doing that?


Solution 1:

The closest you can get is to parse the variables into an associative array first thing every line. That is to say,

awk '{ delete vars; for(i = 1; i <= NF; ++i) { n = index($i, "="); if(n) { vars[substr($i, 1, n - 1)] = substr($i, n + 1) } } Var = vars["Var"] } { print Var, $5 }'

More readably:

{
  delete vars;                   # clean up previous variable values
  for(i = 1; i <= NF; ++i) {     # walk through fields
    n = index($i, "=");          # search for =
    if(n) {                      # if there is one:

                                 # remember value by name. The reason I use
                                 # substr over split is the possibility of
                                 # something like Var=foo=bar=baz (that will
                                 # be parsed into a variable Var with the
                                 # value "foo=bar=baz" this way).
      vars[substr($i, 1, n - 1)] = substr($i, n + 1)
    }
  }

  # if you know precisely what variable names you expect to get, you can
  # assign to them here:
  Var     = vars["Var"]
  Version = vars["Version"]
  Len     = vars["Len"]
}
{
  print Var, $5                  # then use them in the rest of the code
}

Solution 2:

$ cat file | sed -r 's/[[:alnum:]]+=/\n&/g' | awk -F= '$1=="Var"{print $2}'
Howdy Other

Or, avoiding the useless use of cat:

$ sed -r 's/[[:alnum:]]+=/\n&/g' file | awk -F= '$1=="Var"{print $2}'
Howdy Other

How it works

  • sed -r 's/[[:alnum:]]+=/\n&/g'

    This places each key,value pair on its own line.

  • awk -F= '$1=="Var"{print $2}'

    This reads the key-value pairs. Since the field separator is chosen to be =, the key ends up as field 1 and the value as field 2. Thus, we just look for lines whose first field is Var and print the corresponding value.