串联打印范围

2024-09-30 08:27:40 发布

您现在位置:Python中文网/ 问答频道 /正文

提取为单个字符串

Number 1:  
Number 2:  55-60(a)
Number 3:  43/32455, 225(c), 26/10533-10541
Number 4:  234(b) 
Number 5:  
Number 6:  643/54231 
Number 7:  10(a), 162(b) 
Number 8:  742/12345,12346
(c=cross,b=bold,a=alpha)

值为“数字4”的数字将是234c4 等等:

Number 4:  234(b) 

"4" or any digit with Number [0-9], will be concatenated with c = c + 4
234 is the value for the Number 4, then 234 + c4 = 234c4 (flagged as b = bold)


Number 6:  643/54231 
value without any flag will be sorted into the cross column along with cross tag (c)

Sorted : 
cross
43c3@SP32455;225c3;643c6@SP54231

打印范围

Number 2: 55-60(a)
Sorted :
alpha
55c2;56c2;57c2;58c2;59c2;60c2

Number 3:  26/10533-10541
cross
26c3@SP10533;26c3@SP10534;26c3@SP10535;26c3@SP10536;26c3@SP10537;26c3@SP10538;26c3@SP10539;26c3@SP10540;26c3@SP10541

一旦列出所有标记的输出(alpha、bold、cross/a、b、c)。 等等:

a / alpha =
55c2;56c2;57c2;58c2;59c2;60c2;10c7
b / bold = 
234c4;162c7
c / cross = 
43c3@SP32455;225c3;26c3@SP10533;26c3@SP10534;26c3@SP10535;26c3@SP10536;26c3@SP10537;26c3@SP10538;26c3@SP10539;26c3@SP10540;26c3@SP10541;643c6@SP54231;742c8@SP12345;742c8@SP12346

它将合并成一行作为预期输出(逗号分隔),如下所示

a,b,c
55c2;56c2;57c2;58c2;59c2;60c2;10c7,234c4;162c7,43c3@SP32455;225c3;26c3@SP10533;26c3@SP10534;26c3@SP10535;26c3@SP10536;26c3@SP10537;26c3@SP10538;26c3@SP10539;26c3@SP10540;26c3@SP10541;643c6@SP54231;742c8@SP12345;742c8@SP12346


交叉检查的细目:

alpha
55c2;56c2;57c2;58c2;59c2;60c2;10c7
bold
234c4;162c7
cross
43c3@SP32455;225c3;26c3@SP10533;26c3@SP10534;26c3@SP10535;26c3@SP10536;26c3@SP10537;26c3@SP10538;26c3@SP10539;26c3@SP10540;26c3@SP10541;643c6@SP54231;742c8@SP12345;742c8@SP12346

现在,我使用一行脚本打印范围

for a in {10533..10541}; do echo $a | awk '{printf "26c3@SP%s;",$0}'; done

Tags: alphanumberboldcrosssp10534sp10536sp10540sp32455
1条回答
网友
1楼 · 发布于 2024-09-30 08:27:40

这是纯awk中的一个解决方案。它不是特别可读,我不相信awk是这个特殊情况下工作的正确工具。可能需要调整一些模式和/或行动。我是根据您的具体案例来制定解决方案的

BEGIN { RS = "\n|, |:  " }

$1 ~ /^Number/                 { sub(":", "", $2); current = $2 }
$1 ~ /^[0-9]+-[0-9]+\(a\)$/    { sub(/\(a\)$/, "", $1);
                                 split($1, a, "-")
                                 for (i=a[1]; i <= a[2]; i++)
                                   alpha = alpha (alpha == "" ? "" : ";") sprintf("%dc%d", i, current)
                               }
$1 ~ /^[0-9]+\(a\)$/           { sub(/\(a\)$/, "", $1)
                                 alpha = alpha (alpha == "" ? "" : ";") sprintf("%dc%d", $1, current)
                               }
$1 ~ /^[0-9]+\(b\)$/           { sub(/\(a\)$/, "", $1)
                                 bold = bold (bold == "" ? "" : ";") sprintf("%dc%d", $1, current)
                               }
$1 ~ /^[0-9]+\/[0-9]+$/        { split($1, a, "/")
                                 cross = cross (cross == "" ? "" : ";") sprintf("%dc%d@SP%d", a[1], current, a[2])
                               }
$1 ~ /^[0-9]+\/[0-9]+-[0-9]+$/ { split($1, a, "[/-]")
                                 for (i=a[2]; i <= a[3]; i++)
                                   cross = cross (cross == "" ? "" : ";") sprintf("%dc%d@SP%d", a[1], current, i)
                               }
$1 ~ /^[0-9]+\/[0-9]+,[0-9]+$/ { n = split($1, a, "[/,]");
                                 for (i=2; i <= n; i++)
                                   cross = cross (cross == "" ? "" : ";") sprintf("%dc%d@SP%d", a[1], current, a[i])
                               }

END { print "alpha"; print alpha ; print "bold" ; print bold ; print "cross" ; print cross }

相关问题 更多 >

    热门问题