I want to print a multiline string in colour using lines with colour tags ("Grn:" for green; "Blu:" for blue, ...).
Here is an example multiline string
Code:
mstr=" \n \
Grn: \n \
Some text in green colour \n \
More text here \n \
Blu: \n \
Some Blue text \n \
More blue text"
Here is a function that I have constructed. Where I split the multiline string into an array. What I need to de new is match any of the "tseq[knam]" indices in "knam" when they occur in the string.
It seems that GSUB cannot take the fourth argument.
Code:
function lumer(str) {
kl=0
kmd="tput sgr0"
rst = ( (kmd | getline outp) > 0 ? outp : "<" "sgr0" ">" )
close(kmd)
ka = "Wht 15 Grn 34 Blu 39 Ylw 11 Red 196 Amb 214"
kb = "Cyn 51 Mgn 201 Syp 39 Code 39"
ks = sprintf("%s %s", ka, kb)
n = split(ks, kaggr) # split makes KAGGR start at index 1
for ( i=1; i<n; i+=2 ) {
knam = kaggr[i] ":"
knum = kaggr[i+1]
kmd=sprintf("%s%d\n", "tput bold; tput setaf ", knum)
tseq[knam] = ( (kmd | getline kseq) > 0 ? kseq : "<" knam ">" )
close(kmd)
}
## Split with newline character, and store in array ASTR
nlines = split(mstr, astr, "\n")
for (i = 1; i <= nlines; i++) {
## Capture KNAM from TSEQ[KNAM] and match with string ASTR[I]
for ( knam in tseq ) {
## Remove any blanks from string
str = "" ; gsub(/[[:blank:]]+/, "", astr[i], str)
if ( knam == str ) {
print "Matched line: " astr[i]
}
}
print i " " astr[i]
}
}