By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

any way to extract a value from a text file?

100+
P: 170
hi guys,

i just did printing to a certain file
Expand|Select|Wrap|Line Numbers
  1. #!perl\bin\perl
  2. use strict;
  3. use warnings;
  4.  
  5.   my $t; 
  6.   my @ok;
  7.  
  8.     use Net::Telnet(); 
  9.     $t = new Net::Telnet;
  10.  
  11.      $t->open("ip_address");
  12.      $t->print("command");
  13.      $t->print("command");
  14.      $t->print("command");
  15.      $t->print("commands");
  16.      @ok=$t->getlines(timeout=>300);
  17.  
  18.      open(FILE, ">c:\\documents and settings\\clong\\desktop\\file.txt");
  19.      print FILE ("@ok");
  20.  
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??
Apr 23 '08 #1
Share this Question
Share on Google+
8 Replies


KevinADC
Expert 2.5K+
P: 4,059
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:

Expand|Select|Wrap|Line Numbers
  1. foreach my $line (@ok) {
  2.     $line =~ s/\s(\S+)$/$1/;
  3. }
If it doesn't work just mess around with the regexp until it does.
Apr 23 '08 #2

100+
P: 170
hm.ok
so what should i mess it around with so as to get the correct regexp?
im totally unsure about this
Apr 23 '08 #3

100+
P: 170
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

Expand|Select|Wrap|Line Numbers
  1.     $length_date = index($original,"From (local)");                
  2.     $date_line = substr($original,($length_date + 14));
  3.     $rev_date = scalar reverse $date_line;                
  4.     $date = scalar reverse substr($rev_date,-8);
  5.  
Apr 23 '08 #4

nithinpes
Expert 100+
P: 410
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

Expand|Select|Wrap|Line Numbers
  1.     $length_date = index($original,"From (local)");                
  2.     $date_line = substr($original,($length_date + 14));
  3.     $rev_date = scalar reverse $date_line;                
  4.     $date = scalar reverse substr($rev_date,-8);
  5.  
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(entire 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:00: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:

Expand|Select|Wrap|Line Numbers
  1.     $length_date = index($original,"From (local)");                
  2.     $date = substr($original,($length_date + 14),8);
  3.  
  4.  
Apr 23 '08 #5

nithinpes
Expert 100+
P: 410
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?
Apr 23 '08 #6

100+
P: 170
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
Apr 23 '08 #7

nithinpes
Expert 100+
P: 410
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?
Apr 23 '08 #8

100+
P: 170
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
Apr 23 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.