Passing Variable to Regular Expression

Hi All,

Below is a sample 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

 
cat AIDNameList.txt | grep "${Name}Aid" | while read line
do
     print $line;
done
1 Like

Much simpler Perl version

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;
1 Like

Thank you all for the reply ... but somehow it is not working .. I am trying to tune it .. :b:

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

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

Can u post sample input string and existence of the same in input file to get exact idea on your problem?

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.

Sorry, my bad, you'll have to chomp the input, otherwise it'll include the newline character in the search:

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 man grep (POSIX)?

1 Like
read name
grep "^$name" AIDNameList.txt

Even this will satisfy your requirement. Please correct if I missed judged your point.

It worked now. :cool:

---------- Post updated at 04:57 PM ---------- Previous update was at 04:53 PM ----------

Thank u So much .. I simply didnot somehow consider using grep.

sorry jisha for stealing your thread, i did 1 line change making chomp when in accept the input. i use this frequently but i have not seen in actual coding. can someone tell me reason ? if there is any reason behind it.

I used to get unexpected output before chomp was used. chomp removes the trailing strings usually the new line character. it should be used because a string with a newline character and same string with out the new line charecter are different.
eg: $Name = "Alina"; and $Name = "Alina " are different .

Regular expression search will not return the intended value if chomp is not used in these cases .

i know why chomp is used, but i want to know, why its used like below

my $input = <STDIN> ; 
chomp $input ; 

and not like below

chomp ( my $input = <STDIN> ) ; 

Habit, mostly. I like my variables declared before I pass them to any functions. But as always TMTOWTDI.