I have executed a database command and have generated an output file of the results. Each field name starts with dbt_xxxxxx Each field value is the value after the equal sign. how can I parse the file and write out a new file where the field name is in on column and the value is another column and create a header for the file

dbt_dbid=4   dbt_stat=0x0 (0x0000)
dbt_extstat=0x0 (0x0000)
dbt_stat2=0x0 (0x0000)
dbt_stat3=0x20000 (0x00020000 (DBT3_SYSPARTITIONS_EXISTS))
dbt_stat4=0x0 (0x00000000)
dbt_state=0x2(0x0002 (DBST_ACTIVE))   dbt_keep=0  dbt_hdeskeep=0
dbt_next=0x000000002197DE80   dbt_systask_keep=0 dbt_detachxact_keep 0
dbt_lock=0  dbt_dbaid=1
dbt_verstimestamp= May 15 2012  3:37PM   dbt_dbname=TestDB
dbt_nextseq=11  dbt_oldseq=11
dbt_dbinfobuf.dbi_upgdvers=35 ... .dbi_upgd_minor=1720
dbt_dbinfobuf.dbi_sarg_vers=2  dbt_threshstat=0x0
dbt_thresholds=0x00000000219873B8  dbt_thresh_spin=0x000000002011E300
thc_segment   2 thc_level   664 thc_status 0xf  <-- last chance threshold
dbt_nextid=560001995 dbt_nextidstat=0x0
dbt_dflinfo=0x0000000000000000  dbt_dflstat=0x0
dbt_dbts=0x0000 0x00001492   dbt_xdesqueue   next=0x0000000021986C20

Here's what I came up with:

echo -e "Name\tValue" \
    | cat input.txt - \
    | sed -e 's/dbt_/\ndbt_/g;s/=/\t/g' \
          -e 's/^[[:space:]]//;s/[[:space:]]$//;/^$/d'

Now, this leaves the "thc_segment" line alone, and makes the last line dbt_xdesqueue next 0x0000000021986C20. If you want something else, you'll have to specify it.

  • Caption won't appear that way, cause cat will eat echo output and do not printout it again. You can use sed for printing caption. Just add to it -e '1i Name\tValue'. Also seems field name doesn't start only with dbt_. Try to replace like this: s/\([a-z]\+\)_/\n\1_/g – rush May 18 '12 at 21:07
  printf "%s\t%s\n" Name Value
  grep -Po 'dbt_.+?=.*?(?=dbt_|$)' input_file | tr = '\t'
} > output_file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.