Reading unknown word on txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • über
    New Member
    • Sep 2007
    • 31

    Reading unknown word on txt file

    Lets say i have text file like this
    Code:
    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.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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.

    Comment

    • über
      New Member
      • Sep 2007
      • 31

      #3
      I got some code now but it doesnt work
      Code:
      #!/usr/bin/perl
      
      
      $data_file="file.txt";
      open(DAT, $data_file);
      @raw_data=<DAT>;
          while (@raw_data)
          {   if (/Your name is/)
              {   print @raw_data;
              }
          }
      close(DAT);

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        close....

        Code:
        #!/usr/bin/perl
         
         
        $data_file="file.txt";
        open(DAT, $data_file) or die "$!";
        @raw_data=<DAT>;
        for (@raw_data) {
           if (/Your name is (\w+)/i) {
              print "name = $1\n"
           }
        }
        close(DAT);
        it is also recommened to always use "strict" and "warnings" in your perl programs.

        Comment

        Working...