grep output into array

后端 未结 5 1324
清酒与你
清酒与你 2021-02-05 23:29

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 w

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-06 00:32

    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`
    

提交回复
热议问题