rheasam
asked on
Replace a number string in the log and mask part of it
I have entry in the log which reads:
GetTagNo,data,value,123456 789
I need to be able to have a script which runs everynight on the daily log at midnight:
for example log2010-03-31.txt and replaces the above string such that this particular entry gets masked:
GetTagNo,data,value,XXXXX6 789
The script should replace only toddays log: For example logYYYY-MM-DD.txt and then look for the above pattern
The no string can be 5 to 15 digits long.
Can anybiody suggest a vbscript that can be run as aschedule task to do this?
GetTagNo,data,value,123456
I need to be able to have a script which runs everynight on the daily log at midnight:
for example log2010-03-31.txt and replaces the above string such that this particular entry gets masked:
GetTagNo,data,value,XXXXX6
The script should replace only toddays log: For example logYYYY-MM-DD.txt and then look for the above pattern
The no string can be 5 to 15 digits long.
Can anybiody suggest a vbscript that can be run as aschedule task to do this?
ASKER
Yes, the no is always the last field.
Ok, the no of digits can vary. It can be from a 5 digit to 15 digit no. We just need the last 4 to be un masked and rest masked.
Ok, the no of digits can vary. It can be from a 5 digit to 15 digit no. We just need the last 4 to be un masked and rest masked.
The masking part is easy. Here is how I would do it. One solution will preserve the length and the other will mask that as well:
---------------------------------------
Solution 1 Preserve Length
---------------------------------------
str2Mask = "GetTagNo,data,value,123456789"
arr2Mask = split(str2Mask, ",")
str2Mask = arr2Mask(3)
For i = 0 to len(str2Mask) - 4
strMask = strMask & "X"
Next
strMask = strMask & right(str2Mask, 4)
---------------------------------------
Solution 2 Predefined Length
---------------------------------------
str2Mask = "GetTagNo,data,value,123456789"
arr2Mask = split(str2Mask, ",")
strMask = "XXXXXXXXXXX" & right(arr2Mask(3), 4)
Try this.
SourceFilePath = "c:\logs"
DestinationFilePath = "c:\masked"
Set oFS = CreateObject("Scripting.FileSystemObject")
FileName = "\log" & Year(Now) & "-" & Right("0" & Month(Now),2) & "-" & Right("0" & Day(Now),2) & ".txt"
SourceFile = SourceFilePath & FileName
DestinationFile = DestinationFilePath & FileName
Set oSource = oFS.OpenTextFile(SourceFile)
Set oDest = oFS.CreateTextFile(DestinationFile)
Do While Not oSource.AtEndOfStream
data = oSource.ReadLine
mask = ""
output = ""
MyArr = Split(data,",")
unmask = MyArr(UBound(MyArr))
For i = 1 To Len(unmask)-4
mask = mask & "X"
Next
mask = mask & Right(unmask,4)
MyArr(UBound(MyArr)) = mask
For q = 0 To UBound(MyArr)
output = output & MyArr(q) & ","
Next
output = Left(output,Len(output)-1)
oDest.WriteLine(output)
Loop
oSource.Close
oDest.Close
ASKER
Ok, thanks. For example here are my log entries:
are
Ok, the string is not constant, it is part of log and there may be several such string occurences on dif flines with varying length of nos. For example....
.........
test123
GetTagNo,data,value,123456 789
test1234
test1235
GetTagNo,data,value,987654 321
test1234
test1235
....................
the resulting new log should look:
.........
test123
GetTagNo,data,value,XXXXX6 789
test1234
test1235
GetTagNo,data,value,XXXXX4 321
test1234
test1235
....................
are
Ok, the string is not constant, it is part of log and there may be several such string occurences on dif flines with varying length of nos. For example....
.........
test123
GetTagNo,data,value,123456
test1234
test1235
GetTagNo,data,value,987654
test1234
test1235
....................
the resulting new log should look:
.........
test123
GetTagNo,data,value,XXXXX6
test1234
test1235
GetTagNo,data,value,XXXXX4
test1234
test1235
....................
ASKER
exxx1976, Thaks your reply.
where is the part in the source code where you look for the string:GetTagNo
We should be able to define the lines containing only certain strings
We do not want to replace nos on each and every line.
Sorry man, you should have written your original request a bit more clearly then. I don't have time to rewrite this right now, maybe in a few days.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I should have read your expected output.
Change the Do While Loop to the attached and it should present that to you.
Change the Do While Loop to the attached and it should present that to you.
Do While Not oSource.AtEndOfStream
data = oSource.ReadLine
If inStr(data, "GetTagNo") then
mask = ""
output = ""
MyArr = Split(data,",")
unmask = MyArr(UBound(MyArr))
For i = 1 To Len(unmask)-4
mask = mask & "X"
Next
mask = mask & Right(unmask,4)
MyArr(UBound(MyArr)) = mask
For q = 0 To UBound(MyArr)
output = output & MyArr(q) & ","
Next
output = Left(output,Len(output)-1)
Else
output = data
End If
oDest.WriteLine(output)
Loop
ASKER
Thanks SR and exxx. Let me test this and get back to you.
@ sr75 - That won't work. You're going to get errors generated by the fact that the split function isn't going to create an array (the one I'm manipulating the whole rest of the way through), so it's going to blow up.
The whole thing needs to be rewritten.
Also, come to think of it, the array should be reinitialized every line.. I overlooked that originally.
OP - Either way, that's the code that solves the original request. Do with it what you will.
The whole thing needs to be rewritten.
Also, come to think of it, the array should be reinitialized every line.. I overlooked that originally.
OP - Either way, that's the code that solves the original request. Do with it what you will.
That should work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
testing it now. I will update further tomorrow and will promptly award you the points after a succesful test.
ASKER
Guys, give me till Mon for final confirmation and will def award you points on Mon.
Is the number always the last field?
How many digits do you want to remain unmasked?