如何从命令输出的行创建数组

编程入门 行业动态 更新时间:2024-10-20 16:03:27
本文介绍了如何从命令输出的行创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为failedfiles.txt的文件,其内容如下:

I have a file called failedfiles.txt with the following content:

failed1 failed2 failed3

我需要使用grep返回该文件中每一行的内容,并将输出保存在要访问的列表中.所以我想要这样的东西:

I need to use grep to return the content on each line in that file, and save the output in a list to be accessed. So I want something like this:

temp_list=$(grep "[a-z]" failedfiles.txt)

但是,这是我输入

echo ${temp_list[0]}

我得到以下输出:

failed1 failed2 failed3

但是我想要的是什么时候去做

But what I want is when I do:

echo ${temp_list[0]}

要打印

failed1

当我这样做时:

echo ${temp_list[1]}

要打印

failed2

谢谢.

推荐答案

您未创建了数组.您所做的是命令替换,只需将将命令输出到变量.

You did not create an array. What you did was Command Substitution which would simply put the output of a command into a variable.

要创建数组,请说:

temp_list=( $(grep "[a-z]" failedfiles.txt) )

您可能还想参考关于阵列的指南.

更多推荐

如何从命令输出的行创建数组

本文发布于:2023-11-02 14:36:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   命令

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!