Sign in to follow this  
Followers 0
huskies

Read line problem

7 posts in this topic

When I send a command to putty it gets logged, for example

send("zcat /store/raw/remote/2011/05/27/*/ktraw_sdkfhgdifuygd8s7y4iudjkfghkdj* | raw-extract-messages | raw-params -p s | sort -n | uniq |wc -l")

it gets sent as 3 separate lines, and to prove it i did this

$file = fileopen($PuttyLogpath,0)
Local $str = FileReadLine($file, (_FileCountLines($PuttyLogpath)-1))
Consolewrite($str)

Would actually give me the 3rd last line: $ zcat /store/raw/remote/2011/05/27/*/ktraw_sdkfhgdi

Local $str = FileReadLine($file, (_FileCountLines($puttylogpath))) gives me: fuygd8s7y4iudjkfghkdj* | raw-extract-messages | raw-params -p s | sort -n | uni

and Local $str = FileReadLine($file, (_FileCountLines($puttylogpath) + 1 )) gives me:q |wc -l

What I really want is the second last line and I had to do Local $str = FileReadLine($file, (_FileCountLines($puttylogpath) + 2 )) to get it, is there anyway to fix this?


<It Shall Be Done>

Share this post


Link to post
Share on other sites



Perhaps a fair guess that the line of interest is unknown. Perhaps last line, perhaps 2nd last line or whatever line. It is perhaps suitable to read the whole file, I doubt that it is big, and process the contents of the file returned. I am not sure of your regex skill as I will suggest StringRegExp() to do the task.

Here is an example of processing some data in $contents variable. You would use FileRead() to get the contents of the file into the $contents variable. The example will return part of the line which has the string "bananas" and it will capture the rest of the line.

$contents = 'nice day' & @CRLF & _
        'nice bananas are yellow' & @CRLF & _
        'strawberry cream is also nice'
; search for "bananas" and rest of line
$value = StringRegExp($contents, 'bananas.*\n', 3)
For $i = 0 To UBound($value) -1
    MsgBox(0, '', $value[$i])
Next

Now testing this, I get a Msgbox() with the text displayed "bananas are yellow".

Share this post


Link to post
Share on other sites

Hi thanks for the idea, however I'm not sure if it would solve my problem. Let me explain my scope a little bit. I have to do counts on about 20-30 items per datawarehouse and so far I was able to automate putty to input the commands and have them logged in a .log file

What I do is I wait for the phrase [companys1-ro@db17.prod ~] to show up in the log then I input a command that extracts a count for me, After I detect the phrase [companys1-ro@db17.prod ~] again, that means the count has been completed. The number that I want should be the second last line. I would loop it until all of the items are counted in the database.

Here is a sample of the log file

=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2011.06.01 16:57:34 =~=~=~=~=~=~=~=~=~=~=~=

login as: myname.11

Authenticating with public key "myname.11@dev1"

Passphrase for key "myname.11@dev1":

Enter passphrase for key '/home/myname.11/.ssh/id_rsa':

Last login: Wed Jun 1 20:56:23 2011 from 10.17.212.80

[companys1-ro@db17.prod ~]$ zcat /store/raw/remote/2011/05/27/*/ktraw_0dbba17bb0

43497bb795db39cf417773* | raw-extract-messages | raw-params -p s | sort -n | uni

q |wc -l

19

[companys1-ro@db17.prod ~]$ zcat /store/raw/remote/2011/05/27/*/ktraw_186f2b947f

7e4600aeacc85eefc9e771* | raw-extract-messages | raw-params -p s | sort -n | uni

q |wc -l

1326

[companys1-ro@db17.prod ~]$ zcat /store/raw/remote/2011/05/27/*/ktraw_1d7145a51a

894d73b5af8207c8f5bd43* | raw-extract-messages | raw-params -p s | sort -n | uni

q |wc -l

1550

[companys1-ro@db17.prod ~]$ zcat /store/raw/remote/2011/05/27/*/ktraw_1f978c59c1

014beda1ebe82fd0d7819a* | raw-extract-messages | raw-params -p s | sort -n | uni

q |wc -l

575

[companys1-ro@db17.prod ~]$ zcat /store/raw/remote/2011/05/27/*/ktraw_24d4ec2631

e044f0b0050125ee2f8046* | raw-extract-messages | raw-params -p s | sort -n | uni

q |wc -l

[ERROR] could not split 'st2' on '='

406755

[companys1-ro@db17.prod ~]


<It Shall Be Done>

Share this post


Link to post
Share on other sites

So if I understand correct, the last line has "[companys1-ro@db17.prod ~]" in it and the second last line has a number such as "406755" that you want. So if the log is "test.log", then perhaps the code below may help you.

$contents = FileRead('test.log')
If @error Then
    MsgBox(0, '', 'Unable to read file')
Else
    ; find CRLF then capture digits then find CRLF then "[companys1-ro@db17.prod ~\]" then EOF
    $value = StringRegExp($contents, '\r\n(\d*)\r\n\[companys1-ro@db17.prod ~\]\z', 3)
    For $i = 0 To UBound($value) -1
        MsgBox(0, '', $value[$i])
    Next
EndIf

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The message box is not showing up at all, so I tried

$contents = FileRead($PuttyLogpath)
$value = StringRegExp($contents, '\r\n(\d*)\r\n\[companys1-ro@db17.prod ~\]$ \z', 3)
Consolewrite($value)
consolewrite(UBound($value))
    For $i = 0 To UBound($value) -1
        MsgBox(0, '', $value[$i])
    Next

and I got 1 for $value and 0 for Ubound($value)

for every count

Edited by huskies

<It Shall Be Done>

Share this post


Link to post
Share on other sites

The conditions have changed if the RegExp pattern is not working. I tested "text.log" in the current working directory with the exact contents of the log you provided. I get "406755" in the Msgbox. I have no knowledge of how much variation that may occur with the logs that you may need to work with.

Lets try _FileReadToArray() as it will hopefully produce a result expected. The result will be the 2nd last line.

#include  <file.au3>
Global $array, $line, $PuttyLogpath

$PuttyLogpath = 'test.log'
If Not FileExists($PuttyLogpath) Then
    MsgBox(0x30, @ScriptName, '"' & $PuttyLogpath & '" not found')
    Exit 1
EndIf

; read file to array and then show 2nd last element of array
; $array[0] will contain the number of lines read into the array.
; $array[$array[0]] is the last line
_FileReadToArray($PuttyLogpath, $array)
If $array[0] > 1 Then
    $line = $array[ $array[0] - 1 ] ; last line - 1 which is 2nd last line
    MsgBox(0, '2nd last line', $line)
EndIf

Share this post


Link to post
Share on other sites

Hi Mhz

Thanks for the idea of stringregexp, i kinda took your idea and took the cheap way out, but here is my logic, I read the file with _FileReadToArray and reverse it, then I will compare every line with the function StringRegExp with the alphabet, if that line contains a letter then it is not the count I want, so far it is working pretty good. So thanks for the help

$rev = ""   
$lines=_FileReadToArray($PuttyLogpath, $rev) ;reads the text to an array
_ArrayReverse($rev) ;and then reverses it
for $i = 0 to ubound($rev) - 1
    
    $Count = $rev[$i] ;start with the last line

    If StringRegExp($Count, $alphabet,0) = 0 Then  ; if there is a letter in this line, then it's not a count   
        exitloop; this line doesn't have letters, so it's just a numerical count, want to keep this 
    endif
next

<It Shall Be Done>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0