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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Bill PrewCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Affandy KhaleedAuthor Commented:
Thank you very much. Its work fine for me. 😘
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.