How to use variables/array in grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use variables/array in grep command
# 1  
Old 01-05-2009
Lightbulb How to use variables/array in grep command

Hi,

I have a reqmt as i have some values in array and I want to search each value in a file by grep command.
Here goes my scripting:

#!/bin/ksh
set -A ArrayA CENTER LEFT RIGHT
echo "ArrayA contains: ${ArrayAİ*¨}"
grep -e "${ArrayAİ*¨}" filename.txt


The above grep is working for me.
If I want to search a value from a variable in array, it is FAILING.
EG:

#!/bin/ksh
set -A ArrayA CENTER LEFT RIGHT
set -A Filenamearray filename1.txt filename2.txt
grep -e "${ArrayAİ1¨}" ${Filenamearrayİ*¨}

The above grep is NOT WORKING.

Can any one please let me know how to search values in array by using grep command.

Regards
Prashant

Last edited by prashant43; 01-05-2009 at 09:19 AM..
# 2  
Old 01-05-2009
Are you trying to search for the string "CENTER LEFT RIGHT"? Or are you trying to search for ONE of the three words?
# 3  
Old 01-05-2009
CENTER LEFT RIGHT are the members of the array.

I want to search all these members in file
# 4  
Old 01-05-2009
You are saying you want to find any one of them? Then:
Code:
echo ${Array[*]}| tr ' ' '\n' | fgrep -f - filename.txt

# 5  
Old 01-05-2009
Thanks otheusSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to use array variables in shell

Hi, everyone. I wrote a code like this for f in HB021* do program done for f in HB034* do program done for f in HB056* do program done . . (5 Replies)
Discussion started by: xshang
5 Replies

2. Shell Programming and Scripting

'*' vs. '@' in Korn Shell Array Variables

In order to use the shellcurses functions described at: Shell Curses function library I am learning about ksh, which has arrays. My trusty Kochan & Wood book says that for any Korn Shell array AR : ${AR } expands to all the defined array elements, and ${#AR } expands to the number... (3 Replies)
Discussion started by: Clovis_Sangrail
3 Replies

3. Shell Programming and Scripting

array variables

Hi, There are total 100 files in the backup directory with the name format as below: prod_bkp_140611_13_30_05.txt prod_bkp_140611_14_30_05.txt prod_bkp_140611_15_30_05.txt prod_bkp_140611_16_30_05.txt How to use array variables (in perl) to get list of names of the above files ? (13 Replies)
Discussion started by: milink
13 Replies

4. 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

5. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies

6. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

7. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

8. Shell Programming and Scripting

Getting variables into a array.

Hi , Im trying to monitor 2 instancesof a process on our solaris server and trying to do a notification if the returned process size is greater than 500M. Since there are two variables returned I want to make use of arrays to check each and every variable that is stored. the issue that im facing... (2 Replies)
Discussion started by: vivsiv
2 Replies

9. Shell Programming and Scripting

grep command with combined variables in Ksh

Hi, I'd like to grep two variables that are seperated with one space in ksh. var1="Feb" var2="09" I tried to grep "$var1 ""$var2" /usr/file It turn into grep Feb 09 /usr/file when it runs. I got error. Can some one help me with this one? Thanks a lot for your help! (4 Replies)
Discussion started by: cin2000
4 Replies

10. Shell Programming and Scripting

Array variables

hi, how to store array values in unix? and how to access array values? can somebody help? kavitha (2 Replies)
Discussion started by: kavitha
2 Replies
Login or Register to Ask a Question