你好有没有人可以建议如何转置树视图中具有 3 种模式的多行
listb: harold newu/edu
tag: 05.8s
step: sha256asd6f4, size: 1024
step: sha256asd6f5, size: 1024
tag: 06.8s
step: sha256asd6f8, size: 1024
listb: break version
tag: 05.8s
step: sha256asd6c5, size: 720
listb: version/test
not exist
我需要对齐到以下输出
listb: harold newu/edu tag: 05.8s step: sha256asd6f4, size: 1024
listb: harold newu/edu tag: 05.8s step: sha256asd6f5, size: 1024
listb: harold newu/edu tag: 06.8s step: sha256asd6f8, size: 1024
listb: break version tag: 05.8s step: sha256asd6c5, size: 720
listb: version/test not exist
我尝试了这个部分解决方案,但它缺少第三个匹配模式“步骤” awk 根据模式转置行并在之后移动(复制)剩余的列
感谢您编辑您的问题以展示您已经尝试过的内容(这使得提供潜在解决方案变得更加容易);这是一种“更简单”的方法,基于每个“块”中是否存在“步骤”和“不存在”,并且只有两个缩进级别:
awk 'BEGIN{OFS=" "} /^[[:alpha:]]/ {a = $0} /^ / && !/^ / {gsub(/^ {1,}/, "", $0); b = $0} /^ / {gsub(/^ {1,}/, "", $0); c = $0} /step/ {print a, b, c} /not exist/ {print a, b}' file
listb: harold newu/edu tag: 05.8s step: sha256asd6f4, size: 1024
listb: harold newu/edu tag: 05.8s step: sha256asd6f5, size: 1024
listb: harold newu/edu tag: 06.8s step: sha256asd6f8, size: 1024
listb: break version tag: 05.8s step: sha256asd6c5, size: 720
listb: version/test not exist
更好的格式:
awk '
BEGIN {
OFS = " "
}
/^[[:alpha:]]/ {
a = $0
}
/^ / && ! /^ / {
gsub(/^ {1,}/, "", $0)
b = $0
}
/^ / {
gsub(/^ {1,}/, "", $0)
c = $0
}
/step/ {
print a, b, c
}
/not exist/ {
print a, b
}' file
listb: harold newu/edu tag: 05.8s step: sha256asd6f4, size: 1024
listb: harold newu/edu tag: 05.8s step: sha256asd6f5, size: 1024
listb: harold newu/edu tag: 06.8s step: sha256asd6f8, size: 1024
listb: break version tag: 05.8s step: sha256asd6c5, size: 720
listb: version/test not exist
$ cat tst.awk
match($0,/[^[:space:]].*/) {
if ( RSTART > prevRstart ) { indent++ }
else if ( RSTART < prevRstart ) { indent-- }
val[indent] = substr($0,RSTART,RLENGTH)
prevRstart = RSTART
if ( indent == 3 ) { print val[1], val[2], val[3] }
else if ( /not exist/ ) { print val[1], val[2] }
next
}
{ indent = prevRstart = 0 }
$ awk -f tst.awk file
listb: harold newu/edu tag: 05.8s step: sha256asd6f4, size: 1024
listb: harold newu/edu tag: 05.8s step: sha256asd6f5, size: 1024
listb: harold newu/edu tag: 06.8s step: sha256asd6f8, size: 1024
listb: break version tag: 05.8s step: sha256asd6c5, size: 720
listb: version/test not exist
awk '
{gsub(/^ */,"")}
/^listb/{list=$0; next}
/^tag/{tag=$0; next}
/^step/{print list,tag,$0}
/^not exist/{print list,$0}
' file
listb: harold newu/edu tag: 05.8s step: sha256asd6f4, size: 1024
listb: harold newu/edu tag: 05.8s step: sha256asd6f5, size: 1024
listb: harold newu/edu tag: 06.8s step: sha256asd6f8, size: 1024
listb: break version tag: 05.8s step: sha256asd6c5, size: 720
listb: version/test not exist