Replace text in first line of tab-delimited file using Powershell -


i have tab delimited file has 10k+ rows. need change specific field of first(header) record specific value.. using following script, messes format.

$contents = get-content $path -delimiter "`t" $contents[1] = 'replaced text' $contents | out-file $path 

i can see format mess up, not sure how keep file , change need to. also, know if there efficient way.. because concerned first line of file.

i tried different approach, works "ok" introduces blank lines after each line:

$content = get-content $path -delimiter "`n" $content |    foreach-object {      if ($_.readcount -le 1) {        $_ -replace 'a','b'      } else {        $_      }    } |    set-content $path 

one option:

$content = {get-content $path}.invoke()  $content[0].replace('a','b') | set-content $path $content.removeat(0) $content | add-content $path 

using .invoke() on script block causes get-content return collection rather array, simplifies removing first element.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -