Creating variables dynamically and using it in script?

Hi,

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 in advance.

Arrays may be a good idea in this instance. However array implementations vary between the various shells. What shell are you using?

#!/bin/bash

Can pls give an example of doing this? I am trying to use array but have not had success , may be missing something.

Since you have BASH you probably have egrep. It accepts multiple expressions like "a|b|c|d".

You can use IFS to split apart a string on ",", then put it back together with "|"

Since IFS changes how any splitting works in the shell you will want to put it back to what it was after!

#!/bin/bash

OLDIFS="$IFS"

while IFS=":" read FILE STRING
do
        IFS=","
        ARR=( $STRING )
        IFS="|"
        echo egrep "${ARR[*]}" $FILE
done < filename

IFS="$OLDIFS"
$ ./script.sh

egrep a|b|c|d file1
egrep e|f file2
egrep g|h|i|j|k|l file3

$

Remove the 'echo' to have it actually run egrep instead of just printing it.

1 Like

@Corona688 - Nice idea to use egrep, I was going to recommend awk for that bit.

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.

Sorry my bad, I should have been more specific. This is what i see when run the command

egrep a|b|c temp.log
-bash: a: command not found
-bash: b: command not found

The command prompt does not return after that, untill i hit CTRL+C. Is it that egrep is not allowed by my system?

That doesn't work because "a|b|c" is not in quotes. It has to be, or the shell assumes you're running

egrep a | b | c

It freezes because 'egrep a' is not given a filename and tries to read from standard input.

Kudos for trying to test the solution instead of just copypasting though! :slight_smile:

You definitely have egrep.

1 Like

Perhaps the debug output line should have been:

echo egrep \""${ARR
[*]}"\" $FILE

Giving:

$ ./script.sh

egrep "a|b|c|d" file1
egrep "e|f" file2
egrep "g|h|i|j|k|l" file3

$
1 Like

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 .

Thanks in advance ...

Try using the \b word boundries like this:

egrep "\ba\b|\bword\b" files
1 Like

Thanks guys.... you guys are amazing. Please continue the good work.