any way to extract a value from a text file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poolboi
    New Member
    • Jan 2008
    • 170

    any way to extract a value from a text file?

    hi guys,

    i just did printing to a certain file
    [CODE=perl]
    #!perl\bin\perl
    use strict;
    use warnings;

    my $t;
    my @ok;

    use Net::Telnet();
    $t = new Net::Telnet;

    $t->open("ip_addre ss");
    $t->print("command ");
    $t->print("command ");
    $t->print("command ");
    $t->print("command s");
    @ok=$t->getlines(timeo ut=>300);

    open(FILE, ">c:\\docum ents and settings\\clong \\desktop\\file .txt");
    print FILE ("@ok");
    [/CODE]

    so i actually have outputs in my text file say

    CODE SERVICE........ ......... E34

    is there a way i can just extract the value E34 and put it into say a variable $temp??
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    parse the array and modify the lines in the array as necessary before printing to the file. Not sure it this will work but you can try:

    Code:
    foreach my $line (@ok) {
        $line =~ s/\s(\S+)$/$1/;
    }
    If it doesn't work just mess around with the regexp until it does.

    Comment

    • poolboi
      New Member
      • Jan 2008
      • 170

      #3
      hm.ok
      so what should i mess it around with so as to get the correct regexp?
      im totally unsure about this

      Comment

      • poolboi
        New Member
        • Jan 2008
        • 170

        #4
        hm..maybe u can help me explain this
        'cos this was done by another college of mine
        apparently he left and i can't understand what he's doing
        but the way he does it still help me extract the value that i want from a text file
        it's from this line
        but in addition to this line there's like many other lines written in te text file


        From (local) 23.01.08 00:00:00

        [CODE=perl]
        $length_date = index($original ,"From (local)");
        $date_line = substr($origina l,($length_date + 14));
        $rev_date = scalar reverse $date_line;
        $date = scalar reverse substr($rev_dat e,-8);
        [/CODE]

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Originally posted by poolboi
          hm..maybe u can help me explain this
          'cos this was done by another college of mine
          apparently he left and i can't understand what he's doing
          but the way he does it still help me extract the value that i want from a text file
          it's from this line
          but in addition to this line there's like many other lines written in te text file


          From (local) 23.01.08 00:00:00

          [CODE=perl]
          $length_date = index($original ,"From (local)");
          $date_line = substr($origina l,($length_date + 14));
          $rev_date = scalar reverse $date_line;
          $date = scalar reverse substr($rev_dat e,-8);
          [/CODE]
          The portion of code that you posted will extract 23.01.08 from the line.
          - The function index() will return the index(position) of the substring in the string. In this case, it will search for the sub-string "From (local)" and return it's index(index value for 'F' ).
          - The substr() function will create a substring from the main string. In this case, from $original(entir e line?). The second argument is the index to start with(index of 'F'+14 which will reach upto index of 2). From this position to end of string/line, substring will be taken. Hence,
          $date_line='23. 01.08 00:00:00'
          - 'scalar' will change the context of reverse() function to scalar(by default reverse function reverses in list context). Hence,
          $rev_date='00:0 0:00 80.10.32'
          - In the next line, negative index(-8) will start counting from end of string and the substr() will take '80.10.32' . This is again reversed in scalar context to get
          $date ='23.01.08'

          Alternatively, you can use substr() function with three arguments where the third argument will be length of the substring to be taken from the starting index.
          The code below will do the same job as the previous one:

          [CODE=perl]
          $length_date = index($original ,"From (local)");
          $date = substr($origina l,($length_date + 14),8);

          [/CODE]

          Comment

          • nithinpes
            Recognized Expert Contributor
            • Dec 2007
            • 410

            #6
            The sample line and requirement that you posted initially is different from what you posted now.
            Is there any particular pattern of the value that you are looking for in each line and trying to extract?

            Comment

            • poolboi
              New Member
              • Jan 2008
              • 170

              #7
              oh okok..what an explaination

              actually i'm doing some super complex manupilation from a mysql database to another prorietary database

              i'm gonna use a script to telnet into a switch, then print everything out from that switch into a text file.
              after which i need to extract certain values in the file which was the problem i stated in this thread..yeah~
              then when i got this value i'm gonna input into my database

              hopefully u can get the whole picture..more of like an automation process here
              so i'm asking a lot on telnet, extracting infos and stuff
              'cos i totally have no experience in the field of database and perl and i'm like learning it all by myself on the net
              haha..yeah

              Comment

              • nithinpes
                Recognized Expert Contributor
                • Dec 2007
                • 410

                #8
                Originally posted by poolboi
                oh okok..what an explaination

                actually i'm doing some super complex manupilation from a mysql database to another prorietary database

                i'm gonna use a script to telnet into a switch, then print everything out from that switch into a text file.
                after which i need to extract certain values in the file which was the problem i stated in this thread..yeah~
                then when i got this value i'm gonna input into my database

                hopefully u can get the whole picture..more of like an automation process here
                so i'm asking a lot on telnet, extracting infos and stuff
                'cos i totally have no experience in the field of database and perl and i'm like learning it all by myself on the net
                haha..yeah
                You say you are going to extract certain values from the text file. What are those values? Do they fit under any criteria?

                Comment

                • poolboi
                  New Member
                  • Jan 2008
                  • 170

                  #9
                  Originally posted by nithinpes
                  You say you are going to extract certain values from the text file. What are those values? Do they fit under any criteria?
                  oh alright
                  these values dun have any fixed criteria
                  'cos till now i'm stilling waiting for information to be given to me
                  but the values are basically taken from the switch's database
                  they have no fixed values
                  and the commands in the switch do not allow me to extract value from the switche's database
                  so i somehow have to print all the chunk of info from the switches database then using the method i pose earlier on
                  to take out the values that i need
                  yup

                  Comment

                  Working...