Score:0

Skip line at match

jp flag

I am printing a multiline string mstr with colour codes defined in array tseq[knam], with knam being a colour identifier (e.g. Blu:, Grn:, etc).

For instance, with the multiline string 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"

to produce the text appropriately coloured

 Some green text
 More green text

 Moving to blue text
 With more blue text here

The following code identifies the colour and sets the appropriate colour code to cpt. But has the problem that the line that contains the colour declaration (e.g. Grn:, Blu:) in also printed.

Thus I want to skip the value of astr[i] when knam == str. How can I solve this ?

## 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
}

I have introduced continue, and test knam == str again.

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 }
 }
jp flag
I do not think it should be `continue`. How were you planning to use it ?
Freddy avatar
cf flag
I think a `next` should do it to skip to the next record. What I don't understand are the extra `\n \\`'s in your input string and the outer for-loop with the splitting. Each line already contains a newline. But you also haven't shown your complete code. Btw., the task looks very related to this one: [unix.stackexchange.com/questions/735244](https://unix.stackexchange.com/questions/735244).
jp flag
I actually think in needs to be a `break`. For making the multiline string with `gawk`, I was forced to include `\n` to indicate a newline and `\` as a continuation character. Is there any other way to construct a multiline string?
jp flag
The outer loop iterates through the multiline string `mstr` line by line.
jp flag
The task is very related but is here applied in the implementation of an awk function.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.