Creating variables dynamically and using it in script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating variables dynamically and using it in script?
# 1  
Old 10-17-2011
Java 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:

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

Last edited by zxmaus; 10-18-2011 at 03:19 AM.. Reason: added code tags
# 2  
Old 10-17-2011
Arrays may be a good idea in this instance. However array implementations vary between the various shells. What shell are you using?
# 3  
Old 10-17-2011
#!/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.
# 4  
Old 10-17-2011
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!

Code:
#!/bin/bash

OLDIFS="$IFS"

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

IFS="$OLDIFS"

Code:
$ ./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.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 10-17-2011
@Corona688 - Nice idea to use egrep, I was going to recommend awk for that bit.
# 6  
Old 10-18-2011
Thanks, this resolves partial problem but now struck with egrep.

Code:
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.
# 7  
Old 10-18-2011
Quote:
Originally Posted by davidtd
Thanks, this resolves partial problem but now struck with egrep.

Code:
egrep a|b|c|d file1

does not work.
In what way does it "not work". Be specific! Smilie If it's a bug I can probably fix that with more information. If you don't actually have it, say so.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating IN list in PLSQL script dynamically by using shell script

Hi all, I have a PLSQL script which has a IN list where it takes some ids as input. For example SELECT * FROM EMPLOYEE WHERE EMPLOYEE_ID IN (comma separated list ) I want to run this quest inside a shell script but I would like to prepare the IN list dynamically where the employee ids... (1 Reply)
Discussion started by: LoneRanger
1 Replies

2. Shell Programming and Scripting

Dynamically creating file names from portions of another file name

Not sure how to do the following, but any help would be appreciated. Has to be done using C shell (sorry about that). I have about 300 files that I need this done for, but I am only going to give one example. I will just need to know how to execute your solution through some type of loop to get... (2 Replies)
Discussion started by: jclanc8
2 Replies

3. Homework & Coursework Questions

Creating a .profile, displaying system variables, and creating an alias

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Here is what I am supposed to do, word for word from my assignment page: 1. Create/modify and print a... (2 Replies)
Discussion started by: Jagst3r21
2 Replies

4. Shell Programming and Scripting

Creating variables in bash

I am writing some scripts using bash and am wondering if there is a better way to perform the following set of formatting variables. s1=" " s2=" " s3=" " s4=" " s5=" " s6=" " s7=" " s8=" " frmt_titl="${bYl}%s${nClor}\n" frmt1_titl="${s1}$frmt_titl"... (10 Replies)
Discussion started by: kristinu
10 Replies

5. Programming

Dynamically creating structure in C/C++ program

Hi, For one of the project which i am working on i need to write a cpp code such that it will create the structure dynamically instead of reading it from header file. For example we have a program which is reading a binary file according to the structure mentioned in header file. But we... (0 Replies)
Discussion started by: AmbikaValagonda
0 Replies

6. Shell Programming and Scripting

need help with creating directories and variables

i'm trying to write a script that has 2 variables, and uses the 1st variable as a number and the 2nd a name to create directories. so if you typed in ./myscript 5 week, it would create 5 directories named week1 - week5. whenever i run this, i get an error message saying week5 already exists, so i... (3 Replies)
Discussion started by: layne2kim
3 Replies

7. Solaris

Creating script adding 3 different variables in 3 columns

I have 3 variables with different information.. they look like this (row-wise aswell): Variable1 = Roland Kalle Dalius Variable2 = ake123 ler321 kaf434 Variable3 = Richardsen Sworden Lokthar How can I sort them by variable3 alphabetical and add them into the same output so... (0 Replies)
Discussion started by: Prantare
0 Replies

8. Shell Programming and Scripting

Dynamically creating text files using shell script

Hi All, I want to create a shell script which dynamically create text files. i am using the following script $i=1 while do cat > test_$i.txt done but while running the script it was stopping(the cursor not going to next step, i have to enter ctrl+c to make it stop). it is creating only... (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

9. Shell Programming and Scripting

Dynamically Creating and Printing to Files

Hello, If anyone could help me out with this, it would be greatly appreciated. I am trying to dynamically create files and print to them. Here is the code I have to so far. thanks. if (n < 5000 ) { # do nothing } else { n = 0; filenum++; # out = ("out" filenum); } ... (1 Reply)
Discussion started by: Laud12345
1 Replies

10. Shell Programming and Scripting

dynamically creating a variable name

Hi ! I have the following situation - ##First variable variableA=JOB_A ##bunch of other variable JOB_A_RESTART=cleanupJobA JOB_B_RESTART=cleanupJobB JOB_C_RESTART=cleanupJobC now i need a script which would - 1. take the first variable 2. create a new variable name... (2 Replies)
Discussion started by: hsahay
2 Replies
Login or Register to Ask a Question