我已经执行了数据库命令并生成了结果的输出文件。每个字段名称以每个字段值开头,dbt_xxxxxx
每个字段值是等号后面的值。如何解析文件并写出一个新文件,其中字段名称位于列中,值是另一列,并为文件创建标题
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_runstat=0x0(0x0000)
dbt_state=0x2(0x0002 (DBST_ACTIVE)) dbt_keep=0 dbt_hdeskeep=0
dbt_next=0x000000002197DE80 dbt_systask_keep=0 dbt_detachxact_keep 0
dbt_dcompver_default=1
dbt_lock=0 dbt_dbaid=1
dbt_verstimestamp= May 15 2012 3:37PM dbt_dbname=TestDB
dbt_logrows=0
dbt_lastlogbp=0x0000000000000000
dbt_logsema=000000000099EE10
dbt_nextseq=11 dbt_oldseq=11
dbt_dbinfobuf.dbi_logvers=7
dbt_dbinfobuf.dbi_upgdvers=35 ... .dbi_upgd_minor=1720
dbt_dbinfobuf.dbi_dbinfovers=5
dbt_dbinfobuf.dbi_sarg_vers=2 dbt_threshstat=0x0
dbt_thresholds=0x00000000219873B8 dbt_thresh_spin=0x000000002011E300
dbt_maxthresh=256
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_dumpthreadlock=0
dbt_dbts=0x0000 0x00001492 dbt_xdesqueue next=0x0000000021986C20
答案1
这是我想出的:
echo -e "Name\tValue" \
| cat input.txt - \
| sed -e 's/dbt_/\ndbt_/g;s/=/\t/g' \
-e 's/^[[:space:]]//;s/[[:space:]]$//;/^$/d'
现在,这将单独保留“thc_segment”行,并生成最后一行dbt_xdesqueue next 0x0000000021986C20
。如果您想要其他东西,则必须指定它。
答案2
{
printf "%s\t%s\n" Name Value
grep -Po 'dbt_.+?=.*?(?=dbt_|$)' input_file | tr = '\t'
} > output_file