Need to extract value from a variable

Hello All,

I have a variable named as output which holds value as below -

output=[record
  bdfo_len      73
  bdfo_key      "     200000016"
  bdfo_param_no "000"
  parm01        NULL
  parm02        NULL
  parm03        NULL
  parm04        NULL
  parm05        NULL
  parm06        NULL
  parm07        NULL
  parm08        NULL
  parm09        NULL
  parm13        NULL
  parm16        NULL
  parm017       NULL
  parm18        NULL
  parm93        NULL
  parm94        NULL
  parm96        NULL
  parm97        NULL
  parm83        NULL
  header        [record
                  bdfo_run_date     41991
                  bdfo_file_id      "GYFL3027"
                  bdfo_centre_id    "G"
                  bdfo_sbi_file_ind "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"]
  trailer       NULL
  data          NULL]

I need to extract bdfo_run_date value which is '41991' out of this, any pointers will be greatly appreciated.

Quick and dirty sed solution:

echo "${output}" | sed -n '/bdfo_run_date/ s///p'

Quoting ${output} preserves the formatting of the variable. sed is run in "no output" mode until it finds the string you are looking for. It then deletes that string and prints the result. The shell then takes care of the white space.

As I said, it is quick and dirty

Andrew

1 Like

Hello Andrew,
Thank you very much for sed solution, it worked, however if I wish to store the value of bdfo_run_date instead of displaying it, what can exactly be done please?

Thanks in advance !

awk splits on whitespace into fields

echo "$output" | awk '$1=="bdfo_run_date" {print $2}'

To store this in a variable you can use the ` ` or $( ) (replacing the command by its output)

output=`echo "$output" | awk '$1=="bdfo_run_date" {print $2}'`
1 Like

:D:D I worked.. thank you very much !