Bash Scripting - How to grep a file into an array

I have figured out how to grep the file like this:

echo `grep $(date +'%Y-%m-%d') Cos-01.csv | cut -d',' -f1`

The above line does echo the correct information from the lines in which my search criteria is found.
Now I am trying to get that information (Yes, just one column of every line) into an array.

I would like each line to have a place in the array automatically.

EXAMPLE:

MyArray[0]=First Line
MyArray[1]=Second line etc.

I have been trying everything that I could think of and I can't get my array populated.
Is there a way to do the above? grep line by line into an array without a loop?

I wish to thank everyone in advance for their assistance.[/HTML]

---------- Post updated at 12:07 PM ---------- Previous update was at 11:42 AM ----------

I have tried doing this:

MyArray=(`grep $(date +'%Y-%m-%d') Cos-01.csv | cut -d',' -f1`)
echo {$MyArray[1]}

It seems that it is just writing each line into the variable MyArray and the last line is the one that is left. I know that I am missing something really simple but I can't seem to figure it out.

Check this link: Arrays

Try this:

MyArray=(`grep $(date +'%Y-%m-%d') Cos-01.csv | cut -d',' -f1`)
echo {$MyArray[*]}
1 Like

Thank you very much felipe.vinturin. The only thing that changed was it still lists only the last line that was grep'd but with a * in the brackets instead of a 1.

I also thank you for the link, I have been there and that is what got me up to the point that I am at now. I will keep looking over the link that you posted to see if I can figure it out while I wait for other replies.

If you could post part of your file, the result you have now and the expected result, a sample of your problem, then it would be easier to help you! =o)

1 Like

Contents of the Cos-01.csv file:

Adam, 2010-09-17 13:16:21, 2010-09-17 13:16:25,  7,  54, No , Yrd,   , -80,        6,        0,   0.  0.  0.  0,   7, Paid, 
John, 2010-09-17 13:16:21, 2010-09-17 13:16:26,  2,  54, No , Yrd,   , -69,       13,        0,   0.  0.  0.  0,   7, Paid, 
Brad, 2010-09-17 13:16:22, 2010-09-17 13:16:26,  9,  54, No, Yrd,    , -64,       15,       15,   0.  0.  0.  0,   4, Paid, 

Expected Output

Adam <--would be in MyArray[0]
John <--would be in MyArray[1]
Brad <--would be in MyArray[2]

---------- Post updated at 12:35 PM ---------- Previous update was at 12:33 PM ----------

[/COLOR]One thing that I did leave out is that I did declare my array:

declare -a MyArray

I don't know if that was taken into account.

---------- Post updated at 12:37 PM ---------- Previous update was at 12:35 PM ----------

The end goal is to have each column in their own arrays.

First column = MyArray
Second column = Date
etc

It works fine for me:

# cat TestFile.txt
Adam, 2010-09-17 13:16:21, 2010-09-17 13:16:25, 7, 54, No , Yrd, , -80, 6, 0, 0. 0. 0. 0, 7, Paid,
John, 2010-09-17 13:16:21, 2010-09-17 13:16:26, 2, 54, No , Yrd, , -69, 13, 0, 0. 0. 0. 0, 7, Paid,
Brad, 2010-09-17 13:16:22, 2010-09-17 13:16:26, 9, 54, No, Yrd, , -64, 15, 15, 0. 0. 0. 0, 4, Paid,
# MyArray=(`grep $(date +'%Y-%m-%d') TestFile.txt | cut -d',' -f1`)
# echo ${MyArray[*]}
Adam John Brad

There was a typo in my last post.

# It was:
echo {$MyArray[*]}

# Must be:
echo ${MyArray[*]}
1 Like

Thank you very much felipe.vinturin. It does work perfectly. I was so close yet so far away.
It is amazing how a couple of () and a $ in the wrong place will break even the best scripts. :slight_smile:

I did notice that someone made my posts all nice and neat. I would like to thank them also. This was my first post and I will do better in my future posts.