loop over characters in input string using awk

前端 未结 4 460
抹茶落季
抹茶落季 2020-12-10 01:31

Believe it or not, I can\'t find the answer to what I would think would be this very basic question.

In awk, how can I loop over the input string character by charac

4条回答
  •  不思量自难忘°
    2020-12-10 01:46

    Not all awk implementations support the above solutions. In that case you could use substr:

    echo here is a string | awk '{
      for (i=0; ++i <= length($0);) 
        printf "%s\n", substr($0, i, 1)
      }'
    

    P.S. In some awk implementations length without arguments defaults to $0, i.e. length and length($0) are equivalent.

提交回复
热议问题