Howdy, Stranger!

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

Categories

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.