SoftTree Technologies SoftTree Technologies
Technical Support Forums
RegisterSearchFAQMemberlistUsergroupsLog in
any grep/egrep equivalent in JAL

 
Reply to topic    SoftTree Technologies Forum Index » 24x7 Scheduler, Event Server, Automation Suite View previous topic
View next topic
any grep/egrep equivalent in JAL
Author Message
splutino



Joined: 02 May 2007
Posts: 21

Post any grep/egrep equivalent in JAL Reply with quote
Is there any equivalent of the grep / egrep unix shell command in JAL
as I can't really find it ...

or which would be the equivalent technique to use?

Thanks
S
Mon Nov 24, 2008 5:48 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7949

Post Reply with quote
Hmm... what exactly do you want to find and where, file, string, memory, database??
Mon Nov 24, 2008 10:29 am View user's profile Send private message
splutino



Joined: 02 May 2007
Posts: 21

Post Reply with quote
to have as result of a

grep pattern_EXPR file

the lines of the file containing the pattern_EXPR

as in any unix shell
Mon Nov 24, 2008 10:39 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7949

Post Reply with quote
It is fairly easy to write such function in JAL, which requires probably 5 to 10 lines of code, open file, read it line by line, search line text for a patter, output somewhere else. It is also very easy to do in VBScript.

For almost ready to use example, see script example for FileRead statement (in that topic press Examples button) . If in that example code you replace MailSend with InStr and some logical check such as for example IfThen, you will get the complete code snippet doing same thing as grep.
Mon Nov 24, 2008 10:48 am View user's profile Send private message
splutino



Joined: 02 May 2007
Posts: 21

Post easy but largely inefficient Reply with quote
I had some spare time and I did as you suggested ..
writing the equivalent funciton in JAL ...

the results are the same but , using Unix Shell grep or even the SFU grep get me the rusults in a fraction of a second,
doing using the FileRead, etc ... takes 45 seconds


the problem is that I am migrating dozens of crontab scripts from a unix shell environment to 24x7 and
this will end up taking too long .. see the code below

Code:
Dim file_number, number
Dim file_number2, number
Dim end_of_file, boolean

Dim source_path, string, "C:\\CLIENTS\\_all_client_folder\\"
Dim target_path, string, "C:\\CLIENTS\\client1\\"

Dim file_to_grep, string "all_clients_data.txt"
Dim grepped_file, string "client1_data.txt"


Dim textpattern, string, "AS8-IMI"
Dim position, number
Dim line_data, string

Concat(source_path,file_to_grep,file_to_grep)
Concat(target_path,grepped_file,grepped_file)
FileSave(grepped_file,"")

// Open recipients file for reading line by line
FileOpen( file_to_grep, "LineMode", "Read", "", file_number )

// Read file line by line until the end of file.

// start checking if the file is empty
EOF( file_number, end_of_file )

FileOpen( grepped_file, "LineMode", "Write", TRUE, file_number2 )

LoopUntil( end_of_file, END_LOOP )

   // start reading each line of the file and save content in line_data
   FileRead( file_number, line_data )
   //search for the textpattern
   InStr (line_data,textpattern,1,position)
   
   If(position, APPEND_LINE, DO_NOTHING)
   APPEND_LINE:
   // Append built line to the outpu file

   FileWrite( file_number2, line_data )

   DO_NOTHING:
   EOF( file_number, end_of_file )

END_LOOP: 

FileClose( file_number2 )
FileClose( file_number )


Am I doing something wrong / very inefficient here ?
Tue Dec 09, 2008 6:18 am View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7949

Post Reply with quote
The code is fine and it should work well for small files. The fact that it takes 45 seconds tell me that the processed file is not small. The solution, if speed is an issue, is to rewrite the same in VBScript and it will run many times faster. I can guarantee at least 10x performance improvement. The bigger the file, the more speed you would gain.

Code:
Sub Main()
   Dim fso, in, out, line

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set in = objFSO.OpenTextFile("my_input_file", 1)
   Set out = objFSO.OpenTextFile("my_output_file", 2)
   Do Until in.AtEndOfStream
        line = in.ReadLine
        if InStr(line, "some text") then out.WriteLine(line)
   Loop
   in.Close
   out.Close
End Sub

In addition, you can also use regular expressions instead of simple InStr, in case you need more sophisticated pattern matching functions.

JAL is not designed for low-level line-by-line or byte-by-byte file processing. JAL jobs should be used as batch like automation scripting tool for performing high level operations FTP, process invocation and manipulation, etc…in other words to automate other stuff automate processes. You can use VBScript jobs as a general purpose scripting tool, which also provide sufficient performance for low-level operations.
Tue Dec 09, 2008 11:53 am View user's profile Send private message
splutino



Joined: 02 May 2007
Posts: 21

Post Reply with quote
Hi, you are right the file is quote big (between 1000 and 2000 lines normally)
but I tried your code but I get a
An error occurred while executing automation script: Processing erros #22 VBScript Error #1010 Line in script: 2 Error message: Expected identifier
any idea?

Code:
Sub Main()
   Dim fso, in, out, line
   
   fso = CreateObject("Scripting.FileSystemObject")
   in = objFSO.OpenTextFile("C:\CLIENTS\folder\filenameALL.txt", 1)
   out = objFSO.OpenTextFile("C:\CLIENTS\client\filename1.txt", 2)
   Do Until in.AtEndOfStream
        line = in.ReadLine
        if InStr(line, "textpattern") then out.WriteLine(line)
   Loop
   in.Close
   out.Close
End Sub

Tue Dec 09, 2008 12:48 pm View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7949

Post Reply with quote
Please try

Code:
Sub Main()
   Dim fso, in, out, line
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set in = fso.OpenTextFile("C:\CLIENTS\folder\filenameALL.txt", 1)
   Set out = fso.CreateTextFile("C:\CLIENTS\client\filename1.txt", true)
   Do Until in.AtEndOfStream
        line = in.ReadLine
        if InStr(line, "textpattern") then out.WriteLine(line)
   Loop
   in.Close
   out.Close
End Sub

Tue Dec 09, 2008 12:56 pm View user's profile Send private message
splutino



Joined: 02 May 2007
Posts: 21

Post Reply with quote
still the same error message ...
some sort of error @ line 2: Expected identifier


what is wrong in line 2?
Tue Dec 09, 2008 1:42 pm View user's profile Send private message
splutino



Joined: 02 May 2007
Posts: 21

Post Reply with quote
it worked like this

Code:
Sub Main()
   Dim fso
     
   set fso = CreateObject("Scripting.FileSystemObject")
   
   dim file_to_grep
   set file_to_grep = fso.OpenTextFile("C:\CLIENTS\GENERAL\big_file.txt", 1)
   
   dim grepped_file
   set grepped_file = fso.CreateTextFile("C:\CLIENTS\CLIENT\client_file.txt", true)
   
   dim current_line
   Do Until file_to_grep.AtEndOfStream
        current_line = file_to_grep.ReadLine
        if InStr(current_line, "txtpattern") then grepped_file.WriteLine(current_line)
  Loop
   grepped_file.Close
   file_to_grep.Close
End Sub


regards
S
Tue Dec 09, 2008 1:53 pm View user's profile Send private message
splutino



Joined: 02 May 2007
Posts: 21

Post Reply with quote
and total time within 1 sec

thanks
S
Tue Dec 09, 2008 1:54 pm View user's profile Send private message
SysOp
Site Admin


Joined: 26 Nov 2006
Posts: 7949

Post Reply with quote
Great. Thanks for the status update.

PS. I fail to understand why comma separated variable list declaration fails. It is syntaxically correct.
Tue Dec 09, 2008 2:05 pm View user's profile Send private message
Display posts from previous:    
Reply to topic    SoftTree Technologies Forum Index » 24x7 Scheduler, Event Server, Automation Suite All times are GMT - 4 Hours
Page 1 of 1

 
Jump to: 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


 

 

Powered by phpBB © 2001, 2005 phpBB Group
Design by Freestyle XL / Flowers Online.