Bash 在变量或数组的每三个字段后面插入一个逗号 (,)?

发布于 2024-08-16 09:45:28 字数 183 浏览 5 评论 0原文

我有一个包含以下内容的变量:“abcdefghijk l”,您如何在每个第三个成员后面添加一个逗号符号(,),使其看起来像这样:“abc,def ,ghi,jk l”

最初我所有的变量数据都存储在一个数组中,所以如果有人知道如何直接操作该数组,那就太好了。

提前致谢

I have a variable with the following contents: "a b c d e f g h i j k l", how would you go about adding a comma sign (,) after each third member so it looks like this: "a b c, d e f, g h i, j k l".

Initially all my variable data is stored in an array so if anyone knows how to directly manipulate the array, it would be great.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

停顿的约定 2024-08-23 09:45:28

awk

$ echo "a b c d e f g h i j k l" | awk '{for(i=1;i<NF;i++)if(i%3==0){$i=$i","}  }1'
a b c, d e f, g h i, j k l

awk

$ echo "a b c d e f g h i j k l" | awk '{for(i=1;i<NF;i++)if(i%3==0){$i=$i","}  }1'
a b c, d e f, g h i, j k l
听风吹 2024-08-23 09:45:28

在 Bash 中:

arr=(a b c d e f g h i j k l)
ind=("${!arr[@]}")    # get the indices of the array (handles sparse arrays)
ind=(${ind[@]:0:${#ind[@]} - 1})    # strip off the last one
# add commas to every third one (but the last)
for i in "${ind[@]}"; do if (( i%3 == 2 )); then arr[i]+=","; fi; done
echo "${arr[@]}"  # print the array
declare -p arr    # dump the array

结果:

a b c, d e f, g h i, j k l
declare -a arr='([0]="a" [1]="b" [2]="c," [3]="d" [4]="e" [5]="f," [6]="g" [7]="h" [8]="i," [9]="j" [10]="k" [11]="l")'

如果您不介意最后一个元素也有逗号,则可以更直接地使用索引(省略设置 $ind 的行):

for i in "${!arr[@]}"; do if (( i%3 == 2 )); then arr[i]+=","; fi; done

如果您不担心关于数组稀疏:

for ((i=0; i<${#arr[@]}-1; i++)); do if (( i%3 == 2 )); then arr[i]+=","; fi

这与 Ghostdog74 的答案基本相同,只是 Bash 数组是从零开始的,而 awk 字段是从一开始的。

In Bash:

arr=(a b c d e f g h i j k l)
ind=("${!arr[@]}")    # get the indices of the array (handles sparse arrays)
ind=(${ind[@]:0:${#ind[@]} - 1})    # strip off the last one
# add commas to every third one (but the last)
for i in "${ind[@]}"; do if (( i%3 == 2 )); then arr[i]+=","; fi; done
echo "${arr[@]}"  # print the array
declare -p arr    # dump the array

Results in:

a b c, d e f, g h i, j k l
declare -a arr='([0]="a" [1]="b" [2]="c," [3]="d" [4]="e" [5]="f," [6]="g" [7]="h" [8]="i," [9]="j" [10]="k" [11]="l")'

If you don't mind the last element also having a comma, you can use the indices more directly (omit the lines that set $ind):

for i in "${!arr[@]}"; do if (( i%3 == 2 )); then arr[i]+=","; fi; done

If you're not worried about the array being sparse:

for ((i=0; i<${#arr[@]}-1; i++)); do if (( i%3 == 2 )); then arr[i]+=","; fi

which is basically the same as ghostdog74's answer except that Bash arrays are zero-based and awk fields are one-based.

思念满溢 2024-08-23 09:45:28

或者:

$ a=(a b c d e f g h i j k l)
$ printf '%s\n' "${a[@]}"|paste -sd'  ,'
a b c,d e f,g h i,j k l

Or:

$ a=(a b c d e f g h i j k l)
$ printf '%s\n' "${a[@]}"|paste -sd'  ,'
a b c,d e f,g h i,j k l
翻身的咸鱼 2024-08-23 09:45:28

这可能对你有用:

 echo "a b c d e f g h i j k l" | sed 's/\(\w \w \w\) /\1, /g'

This might work for you:

 echo "a b c d e f g h i j k l" | sed 's/\(\w \w \w\) /\1, /g'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文