Score:-2

gsub not accepting fourth argument in gawk

jp flag

Have been using gsub when using gawk, but is not accepting the 4th argument. How can I fix this so that I get a string without blanks ?

  for ( knam in tseq ) {
    ## Remove any blanks from array element astr[i]
    str = "" ; gsub(/[[:blank:]]+/, "", astr[i], str)
    if ( knam == str ) {
      print "Matched line: " astr[i]
     }
   }
Score:0
hr flag

At least as of GNU Awk 5.1.0, the gsub function takes only 3 arguments, modifies the third argument in place, and returns the number of substitutions made.

If you want to assign the result to a new string and leave the input unmodified, you can use gensub instead

str = gensub(/[[:blank:]]/,"", "g", astr[i])

However note that while gsub is portable, gensub is a GNU awk extension - if you don't need the advanced features of gensub (such as back-substitution) then use gsub but assign the string first:

str = astr[i]; gsub(/[[:blank:]]+/, "", str)

See The GNU Awk User's Guide: String-manipulation functions.

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.