Link to home
Start Free TrialLog in
Avatar of rheasam
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,123456789

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,XXXXX6789

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?
Avatar of exx1976
exx1976
Flag of United States of America image

I can write one for you.  A couple questions:

Is the number always the last field?

How many digits do you want to remain unmasked?
Avatar of rheasam
rheasam

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.
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)

Open in new window

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

Open in new window

Avatar of rheasam

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,123456789
test1234
test1235
GetTagNo,data,value,987654321
test1234
test1235
....................


the resulting new log should look:
.........
test123
GetTagNo,data,value,XXXXX6789
test1234
test1235
GetTagNo,data,value,XXXXX4321
test1234
test1235
....................


Avatar of rheasam

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
Avatar of sr75
sr75
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should have read your expected output.

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

Open in new window

Avatar of rheasam

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.
That should work.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rheasam

ASKER

testing it now.  I will update further tomorrow and will promptly award you the points after a succesful test.
Avatar of rheasam

ASKER

Guys, give me till Mon for final confirmation and will def award you points on Mon.