
Raju_P
Novice
Dec 7, 2012, 11:15 PM
Post #1 of 4
(1921 views)
|
Regular expression to grep the contetnts of a file.
|
Can't Post
|
|
Hi, Can some one assist me, how to write the regular expression to select the contents of a line from mytxt.txt file Contents of mytxt.txt. 1. My Name, ID123 2. His Name, ID456 3. Her Name, ID789 By running the below program, by entering option 1, $Variable should be assigned with My Name, ID123 ------------------------------------------------------------------------------------------------------ my (@list, $index, $variable, @user); open(LIST, "mytxt.txt") or die "Cannot open mytxt.txt file. $!\n"; @list = <LIST>; print @list; close LIST; print "Enter option: "; #If option 1 is entered then $Variable should assign My Name, ID123. #If option 2 is entered then $Variable should assign His Name, ID456. chomp($index = <STDIN>); while (@list) { chomp; if (/(?<=$index.\s)(.+)/)---------> (I have used look behind option) { $Variable = $2; } } print $Variable; ------------------------------------------------------------------------------------------------------------------
(This post was edited by Raju_P on Dec 8, 2012, 12:47 AM)
|