 |
SoftTree Technologies
Technical Support Forums
|
|
Author |
Message |
splutino
Joined: 02 May 2007 Posts: 21
|
|
any grep/egrep equivalent in JAL |
|
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 |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7949
|
|
|
|
Hmm... what exactly do you want to find and where, file, string, memory, database??
|
|
Mon Nov 24, 2008 10:29 am |
|
 |
splutino
Joined: 02 May 2007 Posts: 21
|
|
|
|
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 |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7949
|
|
|
|
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 |
|
 |
splutino
Joined: 02 May 2007 Posts: 21
|
|
easy but largely inefficient |
|
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
 |
 |
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 |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7949
|
|
|
|
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.
 |
 |
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 |
|
 |
splutino
Joined: 02 May 2007 Posts: 21
|
|
|
|
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?
 |
 |
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 |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7949
|
|
|
|
Please try
 |
 |
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 |
|
 |
splutino
Joined: 02 May 2007 Posts: 21
|
|
|
|
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 |
|
 |
splutino
Joined: 02 May 2007 Posts: 21
|
|
|
|
it worked like this
 |
 |
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 |
|
 |
splutino
Joined: 02 May 2007 Posts: 21
|
|
|
|
and total time within 1 sec
thanks
S
|
|
Tue Dec 09, 2008 1:54 pm |
|
 |
SysOp
Site Admin
Joined: 26 Nov 2006 Posts: 7949
|
|
|
|
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 |
|
 |
|
|
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
|
|
|