Click to See Complete Forum and Search --> : Find string & read & echo text file


SunnyVu
02-28-2007, 03:18 AM
Hi,

I have been reading alot of threads, but couldn't find anyone has done this.

1). I have the php script write the output file called "tmp1.txt". See below.

warning: Remote server talks SSH-1.5 protocol.


Host key not found from database.


Key fingerprint:


xepah-tatuc-velyl-segum-madic-fyket-pilek-hidal-turyv-fabez-tixux


You can get a public key's fingerprint by running


% ssh-keygen -F publickey.pub


on the keyfile.


Are you sure you want to continue connecting (yes/no)? yes
Host key saved to C:/Documents and Settings/LocalService/Application Data/SSH/hostkeys/key_22_server01.pub

host key for server01, accepted by SYSTEM Wed Feb 28 2007 06:33:18

admin's password:
Authentication successful.


Last login: Tue Feb 27 20:34:04 2007 from 10.250.240.202


Sun Microsystems Inc. SunOS 5.8 Generic Patch October 2001

No mail.

Sun Microsystems Inc. SunOS 5.8 Generic Patch October 2001

server01:admin$ cd backup/backup_configs



server01:admin$ server01:admin$ find . -name TEST* -print

./data406/2007-02-22/TESTING1.GZ

./data406/2007-02-21/TESTING1.GZ

./data406/2007-02-23/TESTING1.GZ

./data406/2007-02-24/TESTING1.GZ

./data406/2007-02-26/TESTING1.GZ

./data406/2007-02-27/TESTING1.GZ

./data406/2007-02-25/TESTING1.GZ
server01:admin$

2. I have this code below can read all the lines fine.
$file = "tmp1.txt";
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, 1024);
echo "$data <br>";
}
fclose($fp);

3. however, I am only want to display(echo) the section BOLD above on the page, but I am not able to figure out how to.

Please help,
Thanks in advance.

Paul Jr
02-28-2007, 01:39 PM
If the lines are always in that format, my poor (but tested) RegEx attempt might suite you:

<?php
$file = 'tmp.txt';
$fp = fopen($file, 'r');
while(!feof($fp)) {
$data = fgets($fp, 1024);
if(preg_match('/^\.\/[a-zA-Z0-9]+\/[0-9\-]+\/[A-Z0-9]+\.[a-zA-Z]{2,4}/', $data, $matches)) {
echo $matches[0], '<br />';
}
}
fclose($fp);
?>
But that's assuming it's always going to be in that format (a full-stop followed by a forward slash, followed by any number of letters and numbers, followedby a forward slash, followed by numbers and hyphens only, followed by a forward slash, followed by any number of letters and numbers, followed by a forward slash, followed by another full-stop, followed by 2-4 letters).

Depending on how constant the format is, the regular expression could be made even simpler.

SunnyVu
03-01-2007, 01:14 AM
Hi Paul,

It works great!!!

You are really good, I was hoping that I can learn and using the same technique for other similiar ones, but your codes look so complicated. So I try read the preg_match function and looks like it did the search and filtering, but I have no idea "'/^\.\/[a-zA-Z0-9]+\/[0-9\-]+\/[A-Z0-9]+\.[a-zA-Z]{2,4}/' this mean. Where I need to read to under this?

One more question if you dont mind: If I have something like this:
Key fingerprint:

xepah-tatuc-velyl-segum-madic-fyket-pilek-hidal-turyv-fabez-tixux
You can get a public key's fingerprint by running
% ssh-keygen -F publickey.pub
on the keyfile.

Are you sure you want to continue connecting (yes/no)? yes
Host key saved to C:/Documents and Settings/LocalService/Application Data/SSH/hostkeys/key_22_server01.pub

host key for server01, accepted by SYSTEM Wed Feb 28 2007 06:33:18

admin's password:
Authentication successful.

Last login: Tue Feb 27 20:34:04 2007 from 10.250.240.202
Sun Microsystems Inc. SunOS 5.8 Generic Patch October 2001
No mail.
Sun Microsystems Inc. SunOS 5.8 Generic Patch October 2001

Router1>
Serial0/0 is up, line protocol is up
Hardware is PQUICC with Fractional T1 CSU/DSU
Description: Point-to-point link to Regional Hub Router
Internet address is 1.1.1.1/30
MTU 1500 bytes, BW 1544 Kbit, DLY 20000 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation HDLC, loopback not set, keepalive set (10 sec)
Last input 00:00:00, output 00:00:00, output hang never
Last clearing of "show interface" counters never
Input queue: 0/75/0 (size/max/drops); Total output drops: 0
Queueing strategy: weighted fair
Output queue: 0/1000/64/0 (size/max total/threshold/drops)
Conversations 0/9/256 (active/max active/max total)
Reserved Conversations 0/0 (allocated/max allocated)
5 minute input rate 1000 bits/sec, 1 packets/sec
5 minute output rate 1000 bits/sec, 1 packets/sec
37728315 packets input, 3635068103 bytes, 0 no buffer
Received 3830794 broadcasts, 0 runts, 26 giants, 0 throttles
3042 input errors, 684 CRC, 2231 frame, 0 overrun, 0 ignored, 127 abort
39201958 packets output, 1603611165 bytes, 0 underruns
0 output errors, 0 collisions, 4 interface resets
0 output buffer failures, 0 output buffers swapped out
1 carrier transitions
DCD=up DSR=up DTR=up RTS=up CTS=up
Router1#

How can I do something like: Find the string "Router1>" and also find string "Router1#", then echo the data from "Router1>" to "Router2#"? I have some other ones that I can apply with this technique if it works.

Thank you so much,

Paul Jr
03-01-2007, 10:01 AM
<?php
$file = 'tmp1.txt';
$fp = fopen($file, 'r');
$data = '';
while(!feof($fp)) {
$data .= fgets($fp, 1024);
}
fclose($fp);
$data = explode('Router1>', $data);
$data = explode('Router1#', $data[1]);
echo $data[0];
?>
http://www.regular-expressions.info/

SunnyVu
03-02-2007, 12:22 AM
Thanks alot Paul,

I did split the string and display the section I needed, but it displays in the nasty format like this (concatenate in one line). Any idea?

Serial0/0 is up, line protocol is up Hardware is PQUICC with Fractional T1 CSU/DSU Description: Point-to-point link to Regional Hub Router Internet address is 1.1.1.1/30 MTU 1500 bytes, BW 1544 Kbit, DLY 20000 usec, reliability 255/255, txload 1/255, rxload 1/255 Encapsulation HDLC, loopback not set, keepalive set (10 sec) Last input 00:00:00, output 00:00:00, output hang never Last clearing of "show interface" counters never Input queue: 0/75/0 (size/max/drops); Total output drops: 0 Queueing strategy: weighted fair Output queue: 0/1000/64/0 (size/max total/threshold/drops) Conversations 0/9/256 (active/max active/max total) Reserved Conversations 0/0 (allocated/max allocated) 5 minute input rate 1000 bits/sec, 1 packets/sec 5 minute output rate 1000 bits/sec, 1 packets/sec 37728315 packets input, 3635068103 bytes, 0 no buffer Received 3830794 broadcasts, 0 runts, 26 giants, 0 throttles 3042 input errors, 684 CRC, 2231 frame, 0 overrun, 0 ignored, 127 abort 39201958 packets output, 1603611165 bytes, 0 underruns 0 output errors, 0 collisions, 4 interface resets 0 output buffer failures, 0 output buffers swapped out 1 carrier transitions DCD=up DSR=up DTR=up RTS=up CTS=up

Paul Jr
03-02-2007, 04:44 PM
Replace this line...

$data .= fgets($fp, 1024);

...with this line...

$data .= fgets($fp, 1024) .'<br>';

SunnyVu
03-02-2007, 09:02 PM
It works PERFECT!!!

Thank you very much Paul.

Paul Jr
03-03-2007, 10:44 AM
You're welcome. :)