Passing Variable to Regular Expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing Variable to Regular Expression
# 1  
Old 12-14-2010
CPU & Memory Passing Variable to Regular Expression

Hi All,

Below is a sample code:
Code:
        print "Enter the Name:  ";
        my $Name = <>;
        print "Word is $Name";

        open (FH,"AIDNameList.txt");
        while (<FH>)
        {
                my $line;
                print "Word is $Name";
                for $line(<FH>)
                {
                        for ($line =~ /$NameAid/g )                        {
                                print $line;
                        }
                }
        }
        close FH;

I need to search for the user entered value in Variable $Name and search in each line of the file "AIDNameList.txt". I tried many ways but cudnot find how to give the word in the for loop.

Could some one help me out..

Thanks in advance
JS

Last edited by Franklin52; 12-14-2010 at 08:38 AM.. Reason: Please use code tags, thank you
# 2  
Old 12-14-2010
Code:
 
cat AIDNameList.txt | grep "${Name}Aid" | while read line
do
     print $line;
done

This User Gave Thanks to anurag.singh For This Post:
# 3  
Old 12-14-2010
Much simpler Perl version
Code:
print "Enter the Name:  ";
my $Name = <>;
print "Word is $Name";

open my $FH, '<', 'AIDNameList.txt' or die "Can't read AIDNameList: $!";
print grep { /$Name/ } <$FH>;
close $FH;

This User Gave Thanks to pludi For This Post:
# 4  
Old 12-16-2010
Thank you all for the reply ... but somehow it is not working .. I am trying to tune it .. Smilie

---------- Post updated at 03:02 PM ---------- Previous update was at 12:59 PM ----------

Quote:
Originally Posted by pludi
Much simpler Perl version
Code:
print "Enter the Name:  ";
my $Name = <>;
print "Word is $Name";

open my $FH, '<', 'AIDNameList.txt' or die "Can't read AIDNameList: $!";
print grep { /$Name/ } <$FH>;
close $FH;

I tried but still i dont get the output. the grep command is not taking the value in $Name. I need to search for the value in the variable $Name. Smilie
# 5  
Old 12-16-2010
Can u post sample input string and existence of the same in input file to get exact idea on your problem?
R0H0N
# 6  
Old 12-16-2010
My sample AIDNameList.txt file is as below :
_________________________________
name1 emailid1
name2 emailid2
name3 emaild3

My program is to take input value from used (say "name2") and search for this in the file "AIDNameList.txt" and print the whole line "name2 emailid2
"

I need "only" that particular line to be printed.
# 7  
Old 12-16-2010
Sorry, my bad, you'll have to chomp the input, otherwise it'll include the newline character in the search:
Code:
print "Enter the Name:  ";
my $Name = <>;
chomp $Name;
print "Word is $Name";

open my $FH, '<', 'AIDNameList.txt' or die "Can't read AIDNameList: $!";
print grep { /$Name/ } <$FH>;
close $FH;

Besides, what's wrong with using a regular grep?
This User Gave Thanks to pludi For This Post:
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 store regular expression in a variable?

Hi, Im facing a weird behavior storing regular expression in a vaiable. Let us consider >file_name=CTP02_*.tar when I echo the above variable its displaying below which is file CTP02_*tar but when I create a file with name CTP02_1234.tar, and when I echo $file_name its showing... (3 Replies)
Discussion started by: rdineshredy
3 Replies

2. Shell Programming and Scripting

Regular expression as a variable

I'm trying to use a series of regular expressions as variables but can't get it to behave properly. You can see below what I'm trying to do. Here with lowercase a-z and the same with uppercase, numbers 0-9 and again with a set of special characters, without having to type out every single... (3 Replies)
Discussion started by: 3therk1ll
3 Replies

3. Shell Programming and Scripting

Passing regular expression to nawk

I am trying to test if I can replace a regular expression in 'nawk' with a variable. Please let me know why it is not working. I am using ksh88i on solaris8 I am trying use this test as a building block to filter active external DNS connections. Ideally I want to pass variable defined... (4 Replies)
Discussion started by: kchinnam
4 Replies

4. UNIX for Dummies Questions & Answers

grep with variable and regular expression

i have a command line like this in csh script grep -i "$argv$" which i wanted to select the line ending with string provided as argument but it couldn't interpret the '$' (ending with).. any help? (3 Replies)
Discussion started by: ymc1g11
3 Replies

5. Shell Programming and Scripting

passing a regex as variable to awk and using that as regular expression for search

Hi All, I have a sftp session log where I am transferring multi files by issuing "mput abc*.dat". The contents of the logfile is below - ################################################# Connecting to 10.75.112.194... Changing to: /home/dasd9x/testing1 sftp> mput abc*.dat Uploading... (7 Replies)
Discussion started by: k_bijitesh
7 Replies

6. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

7. Shell Programming and Scripting

Passing in regular expression as a parameter to sed

Hi All, Im using Bash. I have a pipe delimited config file which contains 3 columns, 1st column = location of a file to be chnaged 2 column = expression to find within file 3rd column = string to replace. A script will loop through the contetnts of this file and apply the changes using... (2 Replies)
Discussion started by: satnamx
2 Replies

8. Shell Programming and Scripting

Awk's variable in regular expression

Anyone know how I will use awk's variable in a regular expression? This line of code of mine is working, the value PREMS should be a variable: awk '$1 ~ /PREMS/ { if(length(appldata)+2 >= length($1)) print $0; }' appldata=$APPLDATA /tmp/file.tmp The value of APPLDATA variable is PREMS. ... (2 Replies)
Discussion started by: Orbix
2 Replies

9. Shell Programming and Scripting

compare variable against regular expression?

is it possible? if so, how? i want to check a variable whether is it a number or letter in an if-else statement (6 Replies)
Discussion started by: finalight
6 Replies

10. Shell Programming and Scripting

regular expression using a variable

hello, I use AIX with ISM PILOT, I want to match something with a varible like this : $variable = 10 #this variable is the number of the job "$variable STARTED" # the pattern how can use this variable to match it with the word STARTED Tanks (0 Replies)
Discussion started by: barribar
0 Replies
Login or Register to Ask a Question