Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep output into array

Tags:

arrays

grep

bash

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

like image 747
newbietester Avatar asked Aug 24 '11 17:08

newbietester


People also ask

How to store output of grep in array?

You can use: targets=($(grep -HRl "pattern" .)) Note use of (...) for array creation in BASH. Also you can use grep -l to get only file names in grep 's output (as shown in my command).

Does grep return an array?

grep( array, function [, invert ] )Returns: Array. Description: Finds the elements of an array which satisfy a filter function.

Is the output of grep a string?

The -n option of the “grep” command is used to print the output of the search string with the line number of the file.

What data type does grep return?

The grep command searches a text file based on a series of options and search string and returns the lines of the text file which contain the matching search string.


1 Answers

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]}
like image 85
Ray Toal Avatar answered Oct 20 '22 16:10

Ray Toal