Link to home
Create AccountLog in
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
Avatar of Ganesh Gurudu
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

Avatar of 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..
Avatar of 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

ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you very much. Its work fine for me. 😘