Jump to content

Tera Term output to text file


 Share

Recommended Posts

Is there any way to obtain TTL files format output to a text file ?

example :

TTL :

sendln "hello"

A ='705c00'

sendln 'A'

Text file :

hello

705c00

Due to the Tera Term limitation, I can't close the Tera Term while sending command. What I do is I'm using txt file to send command to multiple Tera Term windows, but some TTL script are written as program language, hence I need to convert before sending it into Tera Term.

Advise please.

Link to comment
Share on other sites

Hello Lex,

I do have a bit of experience in working with Tera Term and I might be able to help you.

Can you post a more detailed info about how the ttl file and the text file should look? Can you attach those files?

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Hello Lex,

I do have a bit of experience in working with Tera Term and I might be able to help you.

Can you post a more detailed info about how the ttl file and the text file should look? Can you attach those files?

Thank you, Enaiman for your assistance. I have attached the TTL script to be converted and my desire output for the conversion.

Reading XnY.ttl.txt

My Desire Output.txt

Link to comment
Share on other sites

Here is a script which will do what you need. It will look for (and keep) the lines with "sendln" and discard the others.

Change the input file and output file path to match your needs - at the moment the script uses a file named "script.ttl" which needs to be in the same folder as the script (@ScriptDir).

#include <array.au3>
#include <file.au3>

Global $TTLarray, $TTLvarArray[100][2]
Global $varTmp, $aCounter = 0, $replace = 0

$myTTLfile = @ScriptDir&"\script.ttl"
$myTXTfile = @ScriptDir&"\script.txt"

_FileReadToArray($myTTLfile, $TTLarray)

For $i = 1 To $TTLarray[0]
    If StringInStr(StringStripWS($TTLarray[$i], 8), "='") Then
        $varTmp = StringSplit($TTLarray[$i], "=")
        $TTLvarArray[$aCounter][0] = StringStripWS($varTmp[1], 3)
        $TTLvarArray[$aCounter][1] = StringReplace($varTmp[2], "'", "")
        $aCounter += 1
    EndIf
Next
ReDim $TTLvarArray[$aCounter][2]

For $i = 1 To $TTLarray[0]
    If StringInStr($TTLarray[$i], "sendln") Then
        $replace = 0
        For $j = 0 To UBound($TTLvarArray)-1
            If StringInStr($TTLarray[$i], "'"&$TTLvarArray[$j][0]&"'") Then
                $TTLarray[$i] = StringReplace($TTLarray[$i], "'"&$TTLvarArray[$j][0]&"'", $TTLvarArray[$j][1])
                $replace += 1
            EndIf
        Next
        If $replace = 0 Then
            $TTLarray[$i] = ""
        Else
            $TTLarray[$i] = StringReplace($TTLarray[$i], "sendln", "")
            $TTLarray[$i] = StringReplace($TTLarray[$i], "'", "")
        EndIf
    Else
        $TTLarray[$i] = ""
        ContinueLoop
    EndIf
Next

If Not FileExists($myTXTfile) Then
    FileClose(FileOpen($myTXTfile, 2))
EndIf
_FileWriteFromArray($myTXTfile, $TTLarray, 1)
ShellExecute($myTXTfile)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Hi Enaiman,

Thank you for your prompt reply. I have try to run to run that script, but I got the following error,

Subscript used with non-Array variable.:

For $i = 1 To $TTLarray[0]

For $i = 1 To $TTLarray^ ERROR

I try to search the remedy and found that removing the "[0]" works, but lead to other error as follow,

Array variable subscript badly formatted.:

ReDim $TTLvarArray[$aCounter][2]

ReDim $TTLvarArray[^ ERROR

It will be great if this script works in converting TTL to TXT ! I'm looking forward , thanks.

Link to comment
Share on other sites

Will this help you?

$string = FileRead("Reading XnY.ttl.txt")
$hex_x = StringRegExpReplace($string, "(?s).*[^*]x ='([[:xdigit:]]+)'[^*]*", "$1")
$hex_y = StringRegExpReplace($string, "(?s).*[^*]y ='([[:xdigit:]]+)'[^*]*", "$1")
$array_x = StringRegExp($string, "sendln 'read 'x'([[:xdigit:]]+)'", 3)
$array_y = StringRegExp($string, "sendln 'read 'y'([[:xdigit:]]+)'", 3)
$new = ""
For $i = 0 To UBound($array_x) - 1
    $new &= "read " & $hex_x & $array_x[$i] &  @CRLF
Next
For $i = 0 To UBound($array_y) - 1
    $new &= "read " & $hex_y & $array_y[$i] &  @CRLF
Next
$hFile = FileOpen("My Desire Output.txt", 2)
FileWrite($hFile, $new)
FileClose($hFile)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks Enaiman ! I got the script running.

Just need to take note that before running this script,I need to declare the variable before hand. Previously I run just using a line of sendln, and it give me a number of error.

I was wondering, I can declare the variable before it read the variable, but what if I'm using if and if-else command at TTL script ? I try and it didn't work out. I'm trying to edit the TTL script to overcome this.

Any advise on this ?

Thanks Enaiman for your help.

Link to comment
Share on other sites

This generates an exact duplicate of the desired output - for regular expression Buffs.

Local $InFile = 'Reading XnY.ttl.txt'
Local $OutFile = "Desired Output.txt"

_TTL_TOText($InFile, $OutFile)

If FileExists($OutFile) Then ShellExecute($OutFile)

Func _TTL_TOText(ByRef $sFileIn, ByRef $sFileOut)
    Local $sInFile, $aX, $aY, $sTemp, $sNew, $hFile
    If FileExists($sFileIn) = 0 Then Return

    $sInFile = StringStripCR(FileRead($sFileIn))
    $aX = StringRegExp($sInFile, "(?i)x\h*=\h*'([a-f0-9]+)'", 3) ; x value
    $aY = StringRegExp($sInFile, "(?i)y\h*=\h*'([a-f0-9]+)'", 3) ; y value

    $sTemp = StringRegExpReplace($sInFile, "(?i)([xy]\h*=\h*'[a-f0-9]+'\v)|(wait[^\v]+)", "")
    $sTemp = StringRegExpReplace($sTemp, "(?i)(.+?x')([a-f0-9]{2})'", "read " & $aX[0] & "\2")
    $sNew = StringRegExpReplace($sTemp, "(?i)(.+?y')([a-f0-9]{2})'", "read " & $aY[0] & "\2")

    $hFile = FileOpen($sFileOut, 10)
    FileWrite($hFile, StringAddCR($sNew))
    FileClose($hFile)

    Return
EndFunc   ;==>_TTL_TOText
Link to comment
Share on other sites

Will this help you?

$string = FileRead("Reading XnY.ttl.txt")
$array= StringRegExp($string, "(x =')([[:xdigit:]]+)[']", 3)
$hex_x = $array[1]
$array= StringRegExp($string, "(y =')([[:xdigit:]]+)[']", 3)
$hex_y = $array[1]
$array_x = StringRegExp($string, "sendln 'read 'x'([[:xdigit:]]+)'", 3)
$array_y = StringRegExp($string, "sendln 'read 'y'([[:xdigit:]]+)'", 3)
$new = ""
For $i = 0 To UBound($array_x) - 1
    $new &= "read " & $hex_x & $array_x[$i] &  @CRLF
Next
For $i = 0 To UBound($array_y) - 1
    $new &= "read " & $hex_y & $array_y[$i] &  @CRLF
Next
ConsoleWrite($new)

Br,

UEZ

Oops , UEZ. I'm too into the Enaiman script and didn't notice that you have posted the reply. Thank you for help.

I try to run your script, but nothing happen. I notice a console write at the end of ,that writes data to the STDOUT stream. But I don't understand how to exact the data.

Please advise and thank you for your help.

Link to comment
Share on other sites

I added the lines to write the output to a new file - check out my post#6.

I cannot say whether it is working for all ttl files generally but it seems to work for your Reading XnY.ttl.txt file. :)

Further I don't know whether the output is ok for you because your My Desire Output.txt file has line feeds in each line but this can be modified easily.

Malkey's code produces the same output you have provided.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

This generates an exact duplicate of the desired output - for regular expression Buffs.

Local $InFile = 'Reading XnY.ttl.txt'
Local $OutFile = "Desired Output.txt"

_TTL_TOText($InFile, $OutFile)

If FileExists($OutFile) Then ShellExecute($OutFile)

Func _TTL_TOText(ByRef $sFileIn, ByRef $sFileOut)
    Local $sInFile, $aX, $aY, $sTemp, $sNew, $hFile
    If FileExists($sFileIn) = 0 Then Return

    $sInFile = StringStripCR(FileRead($sFileIn))
    $aX = StringRegExp($sInFile, "(?i)x\h*=\h*'([a-f0-9]+)'", 3) ; x value
    $aY = StringRegExp($sInFile, "(?i)y\h*=\h*'([a-f0-9]+)'", 3) ; y value

    $sTemp = StringRegExpReplace($sInFile, "(?i)([xy]\h*=\h*'[a-f0-9]+'\v)|(wait[^\v]+)", "")
    $sTemp = StringRegExpReplace($sTemp, "(?i)(.+?x')([a-f0-9]{2})'", "read " & $aX[0] & "\2")
    $sNew = StringRegExpReplace($sTemp, "(?i)(.+?y')([a-f0-9]{2})'", "read " & $aY[0] & "\2")

    $hFile = FileOpen($sFileOut, 10)
    FileWrite($hFile, StringAddCR($sNew))
    FileClose($hFile)

    Return
EndFunc   ;==>_TTL_TOText

Thank you, Malkey. I have run the script, and it works like a charms. And also thank you, UEZ and Enaiman for your help.

Programming is making thing easier for user and harder for programmer isn't ? With all this program, I will look into the conversion of using if-else TTL script to regenerate TXT file.

Again, many thanks.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...