I have a problem that I am trying to solve and would greatly appreciate some input to solve this. I have a file containing variable length of line. Each line in the file has values separated by "," and i need to grep for these values in a some files. For example below is a sample file with 3 lines:
file1:a,b,c,d
file2:e,f
file3:g,h,i,j,k,l
I have gone pass to read this file line by line but how do i pass each value separated by "," to variable and use it in a grep command.
So basically i need to dynamically generate variables and pass it to grep command for further processing like:
for a,b,c
grep "$var" file1
for e,f
grep "$var" file2
for g,h,i,j,k,l
grep "$var" file3
Any help on this would be greatly appreciated. Can we use Arrays?
Thanks, this resolves partial problem but now struck with egrep.
egrep a|b|c|d file1
does not work. Is there any other way i can do this. Only way I can think of is using a loop, to loop over an array, but would appreciate if you have anything else.
In what way does it "not work". Be specific! :wall: If it's a bug I can probably fix that with more information. If you don't actually have it, say so.
Thanks guys...it really helped. Appreciate your help
---------- Post updated 10-19-11 at 04:56 PM ---------- Previous update was 10-18-11 at 05:16 PM ----------
One more question to related to the egrep. Your earlier suggestions work fine, but I have a little more complex issue. Like earlier suggested in am using
egrep "a|b|c|d" file1
and it works fine, but it also get the other matching patterns of a. For example my file also has some text like:
xxxayyy , xyza and a
Currently it gets every thing like: "xxxayyy", "xyza" and "a" and I just want to get "a" from the file and not other text can you please suggest how can i use the above " egrep" to filter these and not .