Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Need to find and extract words from text file Posted by myname182 on 21 Jan 2003 at 4:38 PM
Hello,

I know this is done with regular expressions, but I don't know how. I have text file that contains many lines of information. How would I find the line that contains the word "subject:". Then once that line is found how would i get only the text after the colon in to a variable? For example, let say the line is in row 56 and it looks like this: "subject: ORDER CONFIRMATION". How would I find the subject, and how do I get the "ORDER CONFIRMATION". I need to find the subject and only get the subject.


Report
Re: Need to find and extract words from text file Posted by Jonathan on 22 Jan 2003 at 2:10 PM
Hi,

: I know this is done with regular expressions, but I don't know how. I have text file that contains many lines of information. How would I find the line that contains the word "subject:". Then once that line is found how would i get only the text after the colon in to a variable? For example, let say the line is in row 56 and it looks like this: "subject: ORDER CONFIRMATION". How would I find the subject, and how do I get the "ORDER CONFIRMATION". I need to find the subject and only get the subject.


OK, I'm still trying to get better at reg exp myself, but I think I may have the answer to this one.

open FILE, "<textfile.txt";
while (<FILE>) {
     if (/subject\: (.*)/) {
          $whatyouwant = $1;
     }
}
close FILE;


Let me know if that works, I gotta run deal with something so can't test it right now.

Hope this helps,

Jonathan

-------------------------------------------
Count your downloads:
http://www.downloadcounter.com/
And host your site:
http://www.incrahost.com/
Don't say I never give you anything...

Report
Re: Need to find and extract words from text file Posted by indiabhushan on 3 Mar 2003 at 2:57 AM
Hi,

You can use perl system variable $' to extract particular text from the file.

Bharat
indiabhushan@yahoo.com

: Hello,
:
: I know this is done with regular expressions, but I don't know how. I have text file that contains many lines of information. How would I find the line that contains the word "subject:". Then once that line is found how would i get only the text after the colon in to a variable? For example, let say the line is in row 56 and it looks like this: "subject: ORDER CONFIRMATION". How would I find the subject, and how do I get the "ORDER CONFIRMATION". I need to find the subject and only get the subject.
:
:
:

Report
Re: Need to find and extract words from text file Posted by scng on 13 Mar 2012 at 4:18 PM
I have something smiliar question in perl.
I have a text file, and I would like to search a "1234", but would like to display the keyword from the text file of the number.

file1
------------
Number: 1234
Keyword:
Will do the same
Comment: No need to take action

Number: 2345
Keyword:
Hello World
Comment: Just say Hi

so on.....
---------

I know the Number: 1234, so I can use "grep to search the number"
But I would like to display the Keyword corresponding to the Keyword.

-------
Output:

Number: 1234
Keyword:
Will do the same

Number: 2345
Keyword:
Hello World

---------

Anyone know how to do so? Please help...
Thanks!




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.