由于我的最后一个问题是长期,这是一个具有当前代码级别的精简版本.
简介:我需要接受一个以管道分隔的输入文件,检查以确保存在所有适用的记录类型,添加任何缺失的记录,并验证/更正每个记录类型中的子字段数.
输入记录:
AA|1234|ABCD|EDGFT|TR56BE|~BB||E5TGE|~CC|253641|84597|~DD|78HND|ACBE|||43|~EE|HISBL|78943|~FF|12345|SKIP|~GG|||TYBGFR
AA|2345|CDEF|GFHIT|48UJKK|~CC||3FKTI
记录类型和子字段计数验证文件known_flds条目:
AA~5~req
BB~2~opt
CC~3~opt
DD~6~opt
EE~4~opt
FF~2~skp
GG~4~opt
当前脚本,没有子字段更正:
#!/usr/bin/awk -f
BEGIN { FS=OFS="~" }
FNR==NR {
dflts[$1] = create_empty_field($1,$2)
if( $3 ~ /req|opt/ ) fld_order[++fld_cnt] = $1
fld_rule[$1] = $3
next
}
{
flds = ""
j = 1
for(i=1; i<=fld_cnt; i++) {
j = skip_flds( j )
if($j !~ ("^" fld_order[i])) fld = dflts[fld_order[i]]
else { fld = $j; j++ }
flds = flds (flds=="" ? "" : OFS) fld
}
print flds
}
function create_empty_field(name, cnt, fld, i) {
fld = name
for(i=1; i<=cnt; i++) { fld = fld "|" }
return( fld )
}
function skip_flds(fnum, name) {
name = $fnum
sub(/\|.*$/, "", name)
while(fld_rule[name] == "skp") {
fnum++
name = $fnum
sub(/\|.*$/, "", name)
}
return( fnum )
}
我最初尝试执行子域的验证和更正:
#!/usr/bin/awk -f
BEGIN { FS=OFS="~" }
FNR==NR {
dflts[$1] = create_empty_field($1,$2)
if( $3 ~ /req|opt/ ) fld_order[++fld_cnt] = $1
fld_rule[$1] = $3
next
}
{
flds = ""
j = 1
for(i=1; i<=fld_cnt; i++) {
j = skip_flds( j )
if($j !~ ("^" fld_order[i])) fld = dflts[fld_order[i]]
else { fld = fix_sub($j,$2); j++ }
flds = flds (flds=="" ? "" : OFS) fld
}
print flds
}
function create_empty_field(name, cnt, fld, i) {
fld = name
for(i=1; i<=cnt; i++) { fld = fld "|" }
return( fld )
}
function skip_flds(fnum, name) {
name = $fnum
sub(/\|.*$/, "", name)
while(fld_rule[name] == "skp") {
fnum++
name = $fnum
sub(/\|.*$/, "", name)
}
return( fnum )
}
function fix_sub(rec, num, upd, cnt) {
cnt=split(rec,a,"|")-1
upd=""
if(cnt != num)
{for(i=1;i<=$num;i++)
upd = upd a[i] "|" }
else { upd=$rec }
return(upd)
}
当达到第二个记录类型时,上面导致错误.所以现在我知道我需要从known_flds文件中捕获第二个值,以便将其传递给fix_sub函数.
我将补充:
sub_fld[$1] = $2
在FNR == NR部分,但除此之外,我的大脑只是油炸,我不能前进.
我知道作为一个独立的,fix_sub区域工作.现在我只需要从known_flds中读取值即可通过.
所需的输出是:
AA|1234|ABCD|EDGFT|TR56BE|~BB||~CC|253641|84597|~DD|78HND|ACBE|||43|~EE|HISBL|78943||~GG|||TYBGFR
AA|2345|CDEF|GFHIT|48UJKK|~BB||~CC||3FKTI|~DD||||||~EE||||~GG|||
原始问题:UNIX Shell Script Solution for formatting a pipe-delimited, segmented file
最佳答案 试试这个修改过的脚本
#!/usr/bin/awk -f
BEGIN { FS=OFS="~" }
FNR==NR {
dflts[$1] = create_empty_field($1,$2)
if( $3 ~ /req|opt/ ) {
fld_order[++fld_cnt] = $1
subfld_cnt[$1] = $2
}
fld_rule[$1] = $3
next
}
{
flds = ""
j = 1
for(i=1; i<=fld_cnt; i++) {
j = skip_flds( j )
if($j !~ ("^" fld_order[i])) fld = dflts[fld_order[i]]
else { fld = fix_sub(j); j++ }
flds = flds (flds=="" ? "" : OFS) fld
}
print flds
}
function get_field_name(fnum, name) {
name = $fnum
sub(/\|.*$/, "", name)
return( name )
}
function create_empty_field(name, cnt, fld, i) {
fld = name
for(i=1; i<=cnt; i++) { fld = fld "|" }
return( fld )
}
function skip_flds(fnum, name) {
name = get_field_name(fnum)
while(fld_rule[name] == "skp") {
fnum++
name = $fnum
sub(/\|.*$/, "", name)
}
return( fnum )
}
function fix_sub(fnum, name, cnt, a, scnt, i, upd) {
name = get_field_name(fnum)
cnt = split($fnum, a, "|")-1
scnt = subfld_cnt[ name ]
if(cnt != scnt) {
for(i=1;i<=scnt;i++)
upd = upd a[i] "|"
return( upd )
}
return( $fnum )
}
关键差异:
> subfld_cnt [$1] = $2已添加到FNR == NR块中的req | opt部分(处理known_flds文件)
>添加了get_field_name()函数,该函数返回由其fnum参数指定的字段的第一个子字段.
>从函数skip_flds()调用get_field_name()
>修改了fix_sub()以仅获取fnum(所有其他变量都是函数的本地变量)并在必要时修复子字段管道的数量.现在对它的调用只需要在fix_sub(j)中使用j参数.
fix_sub()的细分更改:
> name = get_field_name(fnum)获取查找的字段名称
>拆分$fnum,并获得拆分计数(留在你的-1调整)
> scnt = subfld_cnt [name]从已添加到known_flds文件处理的数组中获取所需的字段数.这是你缺少的主要部分.
>当cnt!= scnt修复subflds.
>保留在upd设置代码中,但删除了upd =“” – 已经为局部变量完成了.
>个人偏好 – 直接返回任何值而不是其他值.
我得到以下内容:
AA|1234|ABCD|EDGFT|TR56BE|~BB||~CC|253641|84597|~DD|78HND|ACBE|||43|~EE|HISBL|78943
||~GG|||TYBGFR|
AA|2345|CDEF|GFHIT|48UJKK|~BB||~CC||3FKTI|~DD||||||~EE||||~GG||||
它与您想要的输出不完全匹配.不同之处在于最终|在GG领域.我认为你想要的输出缺少它.否则,在所有其他处理之后,只需要删除最终字段的最终管道.