如何使用Powershell在文本文件中更新所有行的列值?

huangapple go评论58阅读模式
英文:

How to update column values in all lines in a text file using Powershell?

问题

34324242342333~22~BB74~001~
34656453535353~22~C23~001~
00900900900889~22~N99~001~
97002021207021~22~F22~001~
23423432423421~22~V99~001~

Powershell 代码:

$FileContent = Get-Content -Path C:\TestFiles\DummyData1.txt
foreach ($Row in $FileContent) {
    $RandomNumber= Get-Random -Minimum 111111111111111 -Maximum 999999999999999
    $fields = $Row.Split("~")
    $fields[0] = $RandomNumber
    $fields -join "~"
} | Set-Content -Path C:\TestFiles\DummyDataUpdated.txt
英文:

I have a text file that has 1000's of lines. And each lines has elements that are separated by tilde(~) delimiter. Now, i want to just change the first column value numbers to a Random/Unique number, i.e 34324242342333. It is of 15 chars length. But this is not working.

34324242342333~22~BB74~001~ 
34656453535353~22~C23~001~ 
00900900900889~22~N99~001~ 
97002021207021~22~F22~001~ 
23423432423421~22~V99~001~ 

Powershell code:

 $FileContent = Get-Content -Path C:\TestFiles\DummyData1.txt
        foreach ($Row in $FileContent) {
         $RandomNumber= Get-Random -Minimum 111111111111111 -Maximum 999999999999999
         $fields =  $Row.Split("~")
         $fields[0] =  $RandomNumber
         $fields -join '~' 
        }  Set-Content | C:\TestFiles\DummyDataUpdated.txt   

答案1

得分: 0

你可以尝试像这样做:

$csv = Import-Csv C:\sourceFile.txt -Delimiter '~' -Header @('colA','colB','colC','colD')
$csv | select @{N='colA';E={(Get-Random -Minimum 111111111111111 -Maximum 999999999999999)}},colB,colC,colD

根据你的需求更改列名。

英文:

you can try something like this:

$csv = Import-Csv C:\sourceFile.txt -Delimiter '~' -Header @("colA","colB","colC","colD")
$csv | select @{N="colA";E={(Get-Random -Minimum 111111111111111 -Maximum 999999999999999)}},colB,colC,colD

Change the columns name to fit your need.

答案2

得分: 0

很不幸,PowerShell不允许直接将foreach语句的输出传送。为了使您现有的代码更改最少地能正常工作,请将foreach语句包装在脚本块中:

$FileContent = Get-Content -Path C:\TestFiles\DummyData1.txt

# 将foreach包装在脚本块中,以便将其传送到Set-Content
& {
    foreach ($Row in $FileContent) {
         $RandomNumber= Get-Random -Minimum 111111111111111 -Maximum 999999999999999
         $fields =  $Row.Split("~")
         $fields[0] =  $RandomNumber
         $fields -join '~' 
    }  
} | Set-Content C:\TestFiles\DummyDataUpdated.txt  

这里的& {…}定义了一个脚本块,并立即执行它,使用了调用运算符&

另外,我已经修复了Set-Content调用中的拼写错误(在文件路径参数之前不应该有管道符号)。


作为一种替代的精简解决方案,可以摆脱临时变量$fileContent,并使用单个管道来避免foreach语句:

Get-Content -Path C:\TestFiles\DummyData1.txt | ForEach-Object {
    $RandomNumber= Get-Random -Minimum 111111111111111 -Maximum 999999999999999
    $fields =  $_.Split("~")
    $fields[0] =  $RandomNumber
    $fields -join '~' 
} | Set-Content C:\TestFiles\DummyDataUpdated.txt   

变量$row已被自动变量$_替换,它表示管道当前处理的元素。

英文:

Unfortunately PowerShell doesn't permit piping the output of a foreach statement directly. To make your existing code working with the least amount of changes, wrap the foreach statement in a scriptblock:

$FileContent = Get-Content -Path C:\TestFiles\DummyData1.txt

# Wrap foreach in a scriptblock to be able to pipe it to Set-Content
& {
    foreach ($Row in $FileContent) {
         $RandomNumber= Get-Random -Minimum 111111111111111 -Maximum 999999999999999
         $fields =  $Row.Split("~")
         $fields[0] =  $RandomNumber
         $fields -join '~' 
    }  
} | Set-Content C:\TestFiles\DummyDataUpdated.txt  

Here & {…} defines a scriptblock and immediately executes it, using the call operator &.

Also I've fixed the typo in the Set-Content call (there should be no pipe symbol before the file path argument).


As an alternative, streamlined solution, get rid of the temporary variable $fileContent and use a single pipeline to avoid the foreach statement:

Get-Content -Path C:\TestFiles\DummyData1.txt | ForEach-Object {
    $RandomNumber= Get-Random -Minimum 111111111111111 -Maximum 999999999999999
    $fields =  $_.Split("~")
    $fields[0] =  $RandomNumber
    $fields -join '~' 
} | Set-Content C:\TestFiles\DummyDataUpdated.txt   

The variable $row has been replaced by automatic variable $_ which denotes the current element processed by the pipeline.

答案3

得分: 0

使用具有脚本块的替换来替换任何不是由数字之外的内容前置的数字:

Get-Content .\Data1.txt -Replace ''(?<!\D.*)\d'', { Random 9 } | Set-Content .\Updated.txt
英文:

Using a replacement with a script block on any digit which is not proceeded by anything but a digit:

Get-Content .\Data1.txt -Replace '(?<!\D.*)\d', { Random 9 } |Set-Content .\Updated.txt   

huangapple
  • 本文由 发表于 2023年6月5日 19:11:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定