grep 输出到数组

发布于 2024-12-01 11:56:50 字数 248 浏览 1 评论 0原文

伙计们我怎样才能做到这一点

`查找 /xyz/abc/music/ |grep def`

我不想将数组存储在任何临时变量中。我们如何直接操作这个数组呢?

所以要获取该数组的第一个元素

echo ${$(`查找 /xyz/abc/music/ |grep def`)[0]} 请帮助我如何实现这一目标

Guys How can I make this work

`find /xyz/abc/music/ |grep def`

I don't want to store the array in any temporary variable. How can we directly operate on this array.

so to get the 1st element of that array

echo ${$(`find /xyz/abc/music/ |grep def`)[0]}
Please help me How I can achieve this

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

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

发布评论

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

评论(5

烈酒灼喉 2024-12-08 11:56:50

将 find 的调用放在数组括号中

X=( $(find /xyz/abc/music/ | grep def) )
echo ${X[1]}
echo ${X[2]}
echo ${X[3]}
echo ${X[4]}

Put the call to find in array brackets

X=( $(find /xyz/abc/music/ | grep def) )
echo ${X[1]}
echo ${X[2]}
echo ${X[3]}
echo ${X[4]}
浮华 2024-12-08 11:56:50

如果你只需要第一个元素(或者更确切地说是行),你可以使用 head

`find /xyz/abc/music/ |grep def | head -n 1`

如果你需要访问任意元素,你可以先存储数组,然后检索元素:

arr=(`find /xyz/abc/music/ |grep def`)
echo ${arr[n]}

但这不会将 grep 输出的每一行放入数组的单独元素中。

如果您关心整行而不是单词,则可以使用 headtail 来完成此任务,如下所示:

`find /xyz/abc/music/ |grep def | head -n line_number | tail -n 1`

If you just need the first element (or rather line), you can use head:

`find /xyz/abc/music/ |grep def | head -n 1`

If you need access to arbitrary elements, you can store the array first, and then retrieve the element:

arr=(`find /xyz/abc/music/ |grep def`)
echo ${arr[n]}

but this will not put each line of grep output into a separate element of an array.

If you care for whole lines instead of words, you can use head and tail for this task, like so:

`find /xyz/abc/music/ |grep def | head -n line_number | tail -n 1`
随遇而安 2024-12-08 11:56:50

尽管有点晚了,最好的解决方案应该是 Ray 的答案,但是您必须将默认的字段分隔符环境变量 IFS 覆盖为换行符,以便将完整的行作为数组字段。填充数组后,您应该将 IFS 切换回原始值。我将扩展射线解决方案:



    # keep original IFS Setting
    IFS_BAK=${IFS}
    # note the line break between the two quotes, do not add any whitespace, 
    # just press enter and close the quotes (escape sequence "\n" for newline won't do)
    IFS="
    "
    X=( $(find /xyz/abc/music/ | grep def) )
    echo ${X[1]}
    echo ${X[2]}
    echo ${X[3]}
    echo ${X[4]}
    # set IFS back to normal..
    IFS=${IFS_BAK}


希望这有帮助

Even though a bit late, the best solution should be the answer from Ray, but you'd have to overwrite the default field separator environment variable IFS to newline for taking complete lines as an array field. After filling your array, you should switch IFS back to the original value. I'll expand Rays solution:



    # keep original IFS Setting
    IFS_BAK=${IFS}
    # note the line break between the two quotes, do not add any whitespace, 
    # just press enter and close the quotes (escape sequence "\n" for newline won't do)
    IFS="
    "
    X=( $(find /xyz/abc/music/ | grep def) )
    echo ${X[1]}
    echo ${X[2]}
    echo ${X[3]}
    echo ${X[4]}
    # set IFS back to normal..
    IFS=${IFS_BAK}


Hope this helps

羁客 2024-12-08 11:56:50

这会起作用

array_name=(`find directorypath | grep "string" | awk -F "\n" '{print $1}'`)
echo $array_name

this will work

array_name=(`find directorypath | grep "string" | awk -F "\n" '{print $1}'`)
echo $array_name
陌路黄昏 2024-12-08 11:56:50

您的意思是获取输出的第一行吗?

find /xyz/abc/music/ |grep def|head 1

Do you mean to get the first line of the output?

find /xyz/abc/music/ |grep def|head 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文