Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need to find and extract words from text file

myname182myname182 Posts: 9Member
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.


Comments

  • JonathanJonathan Posts: 2,914Member
    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.

    [code]
    open FILE, ") {
    if (/subject: (.*)/) {
    $whatyouwant = $1;
    }
    }
    close FILE;
    [/code]

    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... ;-)

  • indiabhushanindiabhushan Posts: 3Member
    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.
    :
    :
    :

  • scngscng Posts: 1Member
    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!

Sign In or Register to comment.