遍历用awk输入字符串中的字符 [英] loop over characters in input string using awk

查看:638
本文介绍了遍历用awk输入字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不管你信不信,我找不到答案我会觉得这是这个非常基本的问题。

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

在awk中,我怎么能遍历由字符输入字符串的字符?比方说,我只是想打印出来。是否有一个数组我可以访问?还是我需要使用SUBSTR?

In awk, how can I loop over the input string character by character? Let's say I just wanted to print them out. Is there an array I can access? Or do I need to use substr?

基本上是这样的:

echo "here is a string" | awk '
{ for(i=0; i<[length of input string]; i++) 
    printf [value at index i in array x]; 
}'

坦率地说,我很尴尬。

Frankly, I'm embarrassed.

推荐答案

您可以将字符串中使用转换为数组拆分

You can convert a string to an array using split:

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

本垂直打印的字符,每行一个。

This prints the characters vertically, one per line.

这篇关于遍历用awk输入字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆