By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Reading unknown word on txt file

P: 31
Lets say i have text file like this
Expand|Select|Wrap|Line Numbers
  1. Your name is x.
We dont know what the "x" is. Could you do a perl script that idenflies the name and then prints it on screen? I was thinking of that u could take all the next letters in the same line where there reads "Your name is". Sorry if i dont got any code with me, this is just too complicated for me.
Nov 20 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
People that do not demonstrate any effort rarely get help with code. This is not one of those rare instances. Lets see what you have tried. You can use a regexp to do this.
Nov 20 '08 #2

P: 31
I got some code now but it doesnt work
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3.  
  4. $data_file="file.txt";
  5. open(DAT, $data_file);
  6. @raw_data=<DAT>;
  7.     while (@raw_data)
  8.     {   if (/Your name is/)
  9.         {   print @raw_data;
  10.         }
  11.     }
  12. close(DAT);
Nov 20 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
close....

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3.  
  4. $data_file="file.txt";
  5. open(DAT, $data_file) or die "$!";
  6. @raw_data=<DAT>;
  7. for (@raw_data) {
  8.    if (/Your name is (\w+)/i) {
  9.       print "name = $1\n"
  10.    }
  11. }
  12. close(DAT);
  13.  
it is also recommened to always use "strict" and "warnings" in your perl programs.
Nov 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.