grep 输出到数组
伙计们我怎样才能做到这一点
`查找 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将 find 的调用放在数组括号中
Put the call to find in array brackets
如果你只需要第一个元素(或者更确切地说是行),你可以使用
head
:如果你需要访问任意元素,你可以先存储数组,然后检索元素:
但这不会将 grep 输出的每一行放入数组的单独元素中。
如果您关心整行而不是单词,则可以使用
head
和tail
来完成此任务,如下所示:If you just need the first element (or rather line), you can use
head
:If you need access to arbitrary elements, you can store the array first, and then retrieve the element:
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
andtail
for this task, like so:尽管有点晚了,最好的解决方案应该是 Ray 的答案,但是您必须将默认的字段分隔符环境变量 IFS 覆盖为换行符,以便将完整的行作为数组字段。填充数组后,您应该将 IFS 切换回原始值。我将扩展射线解决方案:
希望这有帮助
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:
Hope this helps
这会起作用
this will work
您的意思是获取输出的第一行吗?
Do you mean to get the first line of the output?