Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Power Shell Script to replace a word in a file

$
0
0

Hi All,

I am looking for a script to replace the first occurrence of a word in file. For example:

00 00003468            02-06-2013 14:21:14N   1 8031 Idle         15                     Wait For Incoming Phone Call    

3104 0 N   2 8032 Idle          2                     Wait For Incoming Phone Call   

In the above example, i need to replace first "00" with "2?"

I tried with "Foreach-Object {$_ -replace "${Status}", "2?"} |"and its replacing all the occurrence of "00"

How can i modify only the first occurrence?

Also, i need to do this is in a single line.

For example: cmd /c powershell (Get-Content C:\Users\ariy\Desktop\IVRTest.txt) |Foreach-Object {$_ -replace "00", "2?"} |Set-Content C:\Users\ariy\Desktop\IVRTest.txt

This is for automating a process and we need to edit a file in a single go. We thought of using a power shell script. We need to execute this on remote server.

If any other method is available to do the same i am happy to follow that.

Any help is appreciated.

Thanks and Regards

Riyas


Viewing all articles
Browse latest Browse all 15028

Trending Articles