Avatar of Affandy Khaleed
Affandy Khaleed
 asked on

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
VB Script

Avatar of undefined
Last Comment
Affandy Khaleed

8/22/2022 - Mon
Ganesh Gurudu

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

Affandy Khaleed

ASKER
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..
David Johnson, CD

$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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Affandy Khaleed

ASKER
Thank you very much. Its work fine for me. 😘