Grep: Searching with a regex that contains a variable from an array

I'm attempting to grep for lines formatted like this:

["Item","foo",

Where whatever goes in the place of foo is from an array named stuff. The command I have here an example is:

grep -e '^\[\"Item\",\"$stuff[0]\",'

Any suggestions as to why this isn't working?

---------- Post updated at 05:03 PM ---------- Previous update was at 04:17 PM ----------

This was my solution:

grep -e '^\[\"Item\",\"'$stuff[0]'\",'

It's hard to read, but basically I have $stuff[0] outside of the two regex strings. So basically I'm searching like: <["Item",">foo<",> (this isn't an actual notation it's just showing how I'm separately grep-ing for each part of the line.

Try this:

$ stuff[0]="foo"; grep -e "^\[\"Item\",\"${stuff[0]}\"," test.txt
["Item","foo",
1 Like

To add some explanation to spacebar's post if it's not obvious...

To reference an array element you must enclose it in curly braces:

echo ${stuff[0]}

idx=0
echo ${stuff[$idx]}
1 Like

I have the data in table format..

A 345 456 678 878
B 456 789 543 789
A 567 984 234 567
B 546 576 987 133
C 586 986 643 746
A 354 575 263 564

How can I get rows from above data which have data 'A' in first column..{lease help me...

Thanks

Hello,

Please do not ask new questions in existing threads. Unless you have new information to a particular problem, please create a new thread in the appropriate forum.

Also, please search the forums first, as it might be that someone has already posted an answer for a similar problem.

Best regards,
The UNIX and Linux Forums