CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Grep a value from file in perl script

 



kvok
Novice

May 9, 2016, 9:15 AM

Post #1 of 3 (1326 views)
Grep a value from file in perl script Can't Post

 
I need to search in file a.txt and assign to variable $my_var
string value : 989/6xsd/myid

How can I parse it in perl script ?


a.txt

989/6xsd/myid,SF,1,2,3
999/5xxx/myid,FF,2,4,6

thanks


Laurent_R
Veteran / Moderator

May 9, 2016, 10:03 AM

Post #2 of 3 (1325 views)
Re: [kvok] Grep a value from file in perl script [In reply to] Can't Post

This is fairly easy, but you did not specify how we are supposed to recognize what needs to be captured into $my_var? Do you want just the beginning of the first line? Or the beginning of each line successively? Or a string containing '989'? Or something else?

Please clarify your intent, the rest will probably be quite easy.


BillKSmith
Veteran

May 9, 2016, 12:04 PM

Post #3 of 3 (1318 views)
Re: [kvok] Grep a value from file in perl script [In reply to] Can't Post

I assume that you want to simulate the unix 'grep'.


Code
perl -ne"print if m{^989/6xsd/myid}" a.txt


Note: Double quotes are for windows. Substitute single quotes for unix.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives