How to search and replace some word in the same line

I have a text file like :

1,Jane Doe,JKSMFFK332S,JJK332S,OptiPlex 960,2017-12-18 11:02:01
2,Jane Doe,JIBRAIL,4WK332S,OptiPlex 960,2017-12-18 11:04:34
3,Jonathan Davis,JKSGYYO332S,YYO332S,OptiPlex 960,2017-12-18 11:30:25
4,Jane Doe,MIKAIL,GGF332S,OptiPlex 960,2017-12-18 11:56:02

How do i find keyword "Jane Doe,JIBRAIL" and replace the time stamp (2017-12-18 11:04:34) to current time stamp
Affandy KhaleedAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
And since you asked for VBS, here is that approach.  Save this as a file with VBS extension, and then run as follows:

cscript EE29074271.vbs in.txt out.txt

or

cscript EE29074271.vbs in.txt

The first approach will read from in.txt, and write to out.txt.  The second example will read from in.txt and overwrite it with the results of the changes.

' Define needed I/O constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm
If (WScript.Arguments.Count > 0) Then
   strInFile = WScript.Arguments(0)
Else
   WScript.Echo "No input filename specified."
   WScript.Quit
End If

' Get output file name from command line parm (if none, overwrite input file)
If (WScript.Arguments.Count > 1) Then
   strOutFile = WScript.Arguments(1)
Else
   strOutFile = strInFile
End If

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Make sure input file exists
If Not objFSO.FileExists(strInFile) Then
   Wscript.Echo "ERROR: Input file """ & strInFile & """ does not exist."
   Wscript.Quit
End If

' Read entire file contents
Set objFile = objFSO.OpenTextFile(strInFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

' Split input data at line breaks
arrData = Split(strData, vbCrLf)

' Look at each line and reformat dates as needed
For i = 0 To UBound(arrData)

    'Find keyword "Jane Doe,JIBRAIL" and replace the time stamp (2017-12-18 11:04:34) to current time stamp
    If Instr(1, arrData(i), ",Jane Doe,JIBRAIL,", vbTextCompare) Then
        arrData(i) = Left(arrData(i), InstrRev(arrData(i), ",")) & GetDateTime()
    End If

Next

' Write output file with any changes
Set objFile = objFSO.OpenTextFile(strOutFile, ForWriting, True)
objFile.Write Join(arrData, vbCrLf)
objFile.Close


Function GetDateTime()
    ' Format date time string to YYYY-MM-DD hh:mm:ss format
    GetDateTime = Year(Now) & "-" & ZPad(Month(Now), 2) & "-" & ZPad(Day(Now), 2) & " " & ZPad(Hour(Now), 2) & ":" & ZPad(Minute(Now), 2) & ":" & ZPad(Second(Now), 2)
End Function

Function ZPad(strText, intLen)
    ' Pad with leading zeros on the left to specified length
    ZPad = Right(String(intLen, "0") & strText, intLen)
End Function

Open in new window


»bp
0
 
Ganesh GuruduSenior ConsultantCommented:
try like this. this is just a logic,  you need to write the new file by reading  this exiting file.  

Dim strSearchString, strSearchFor
filename = "C:\Temp\vblist.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
strSearchFor = "Jane Doe,JIBRAIL"

Do Until f.AtEndOfStream
	strSearchString =f.ReadLine
	If InStr(1, strSearchString, strSearchFor) > 0 then
	  # Write a code to remove the last comma value and append new time stamp to the current line
	End If
Loop

f.Close

Open in new window

1
 
Affandy KhaleedAuthor Commented:
I'm new in vb script.. can you create for me how to write a code to remove the last comma value and append new time stamp to the current line? thank you for reply..
0
 
David Johnson, CD, MVPOwnerCommented:
$infile = 'ee-29074271.txt'
$data =Get-Content $infile  |  ConvertFrom-Csv -Header number,Name,index,index2,computer,date
$now = get-date -Format ("yyyy-MM-dd HH:mm:ss")
foreach( $dat in $data) 
{
$dat.date = $now

}

$data | Export-csv -NoTypeInformation -Path temp.dat
$dat = get-content temp.dat | select-object -Skip 1 | set-content "temp2.dat"
Remove-Item $infile
Rename-Item -Path 'temp2.dat' -NewName $infile -Force 
Remove-Item -Path temp.dat
get-content $infile | Format-List

Open in new window


as a powershell function

function Get-Data
{
  <#
    .SYNOPSIS
    Changes date to current date/time
    .DESCRIPTION
    The Function will change the date of a specific type of data file to the current date/time
    .EXAMPLE
    Get-Data -infile c:\data\data.txt
    
  #>
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory=$false, Position=0)]
    [System.String] $infile = 'ee-29074271.txt'
    
  )
  
  $data = Get-Content $infile  |  ConvertFrom-Csv -Header number,Name,index,index2,computer,date
  $now = (get-date -Format ("yyyy-MM-dd HH:mm:ss"))
  foreach ($dat in $data) { $dat.date = $now }
  $data | Export-csv -NoTypeInformation -Path temp.dat
  $dat = get-content temp.dat | select-object -Skip 1 | set-content "temp2.dat"
  Remove-Item $infile
  Rename-Item -Path 'temp2.dat' -NewName $infile -Force 
  Remove-Item -Path temp.dat
  $dat = get-content $infile
  foreach($data in $dat){write-verbose $data}
}

Open in new window

0
 
Affandy KhaleedAuthor Commented:
Thank you very much. Its work fine for me. 😘
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.