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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep: Searching with a regex that contains a variable from an array
# 1  
Old 06-22-2012
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:

Code:
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:

Code:
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.
# 2  
Old 06-22-2012
Try this:
Code:
$ stuff[0]="foo"; grep -e "^\[\"Item\",\"${stuff[0]}\"," test.txt
["Item","foo",

This User Gave Thanks to spacebar For This Post:
# 3  
Old 06-22-2012
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:

Code:
echo ${stuff[0]}

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

This User Gave Thanks to agama For This Post:
# 4  
Old 06-26-2012
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
# 5  
Old 06-26-2012
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fastest alternatives to flattening a non-uniform nested array with regex?

Hello, I'm looking at simplfying a function that flattens an array, it uses recursion and filters objects by type but this seems to be a waste of resources to me at least, using conditionals like this seems like a bad idea. The array can be a generic type, int, string, float but not some... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Homework & Coursework Questions

Searching array

I'am writing a program in C language and my code is working perfectly i just need to add a search to it ... My code lets users add companies, and then display them on screen... i would like to add a search that allows user to type company name and then displayall its info on the screen !! THANK... (1 Reply)
Discussion started by: aloushi
1 Replies

3. Shell Programming and Scripting

How to insert an array element within regex?

Hello to all, I'm trying to separate the string "str" using a regex within match function. The substrings that I want to separate, begin with 22, 23, 24 or 25 and followed by 12 or 14 characters. And I want to replace 22 with MJS, 23 with UYT, 24 with WER and 25 with PIL. For this string... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

4. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

5. Shell Programming and Scripting

perl : searching for month and storing the date and time in an array

I am writing the code in perl. I have an array in perl and each variable in the array contains the data in the below format Now I need to check the below variable w.r.t system month I need to store the date and time(Tue Aug 7 03:54:12 2012) from the below data into file if contains only 'Aug'... (5 Replies)
Discussion started by: giridhar276
5 Replies

6. UNIX for Dummies Questions & Answers

| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax

Hello, I'm working on unix with grep (GNU grep) 2.5.1. I'm going through some of the newer regex syntax using Regular Expression Reference - Advanced Syntax a guide. ls -aLl /bin | grep "\(x\)" Which works, just highlights 'x' where ever, when ever. I'm trying to to get (?:) to work but... (4 Replies)
Discussion started by: MykC
4 Replies

7. Shell Programming and Scripting

Perl grep array against array

Hi, Is there any way I can grep an array against another array? Basically here's what I need to do. There will be an array containing some fixed texts and I have to check whether some files contain these lines. Reading the same files over and over again for each different pattern doesnt seem... (1 Reply)
Discussion started by: King Nothing
1 Replies

8. Shell Programming and Scripting

Searching for array in large list of files

I tried to make the title/subject detailed, but well.. have to keep it short as well. I am wanting to take a large list of strings, and search through a large list of files to hopefully find numerous matches. I am not sure the quickest way to do this though. // List of files file1.txt... (2 Replies)
Discussion started by: Rhije
2 Replies

9. Shell Programming and Scripting

Searching array of arrays in perl

Suppose there are two arrays of arrays: @A = ( , , , ); @B = ( , , , , ); For each of $A, $A, $A..., I want to find the corresponding one in @B (match the letter, like $A eq $B), and print out both the second item, for example, $A and $B. How can I do this in perl? grep + map? Hope I... (1 Reply)
Discussion started by: zx1106
1 Replies

10. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies
Login or Register to Ask a Question