我正在打印一个多行字符串mstr
,其中的颜色代码在数组中定义tseq[knam]
,并knam
带有颜色标识符(例如 Blu:、Grn: 等)。
例如,对于多行字符串mstr
,
mstr= "\n \
Grn: \n \
Some green text \n \
More green text \ n \
Blu: \n \
Moving to blue text \n \
With more blue text here"
使文本颜色适当
Some green text
More green text
Moving to blue text
With more blue text here
以下代码识别颜色并将适当的颜色代码设置为cpt
。但问题是,包含颜色声明(例如)的行Grn:
也会Blu:
打印出来。
因此我想跳过astr[i]
when的值knam == str
。我该如何解决这个问题?
## Split with newline character, and store into array
nlines = split(mstr, astr, "\n")
str = astr[i] ; gsub(/[[:blank:]]+/, "", str)
for (i = 1; i <= nlines; i++) {
## Capture KNAM from TSEQ[KNAM] and match with string ASTR[I]
for ( knam in tseq ) {
if ( knam == str ) { cpt = tseq[knam] }
}
print cpt astr[i] rst
}
我已经引入了,并再次continue
测试。knam == str
for (i = 1; i <= nlines; i++) {
## Capture KNAM from TSEQ[KNAM] and match with string ASTR[I]
for ( knam in tseq ) {
if ( knam == str ) { cpt = tseq[knam] ; continue }
}
if ( knam == str ) { print "" } else { print cpt astr[i] rst }
}