如何使用powershell脚本替换文本文件中的值 [英] How to replace a value in text file using powershell script

查看:166
本文介绍了如何使用powershell脚本替换文本文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件包含以下数据(无标题)

My file consists of following data (no header)

DEPOSIT ADD     123456789 (VALUE)(VARIABLE) NNNN    VALUEVARIABLE                                                   
DEPOSIT ADD     234567890 (VALUE)(P75)  NNNN    VALUEVARIABLE                                               
DEPOSIT ADD     345678901 (VALUE)(VARIABLE) NNNN    VALUEVARIABLE           

这是一个制表符分隔的文本文件.共有 5 列.(123456789 (VALUE)(VARIABLE) 是单值列)

This is a tab delimited text file. There are total of 5 columns. (123456789 (VALUE)(VARIABLE) is a single value column)

我的要求是:

  1. 我只需要提取包含 P75 的行以在同一文件中更新.
  2. 在获取 P75 后,我必须替换 Col3、Col4 和 Col5 中的值,其他行应该不受影响.
  1. I need to fetch only the row which contains P75 to update in the same file.
  2. I have to replace the values in Col3,Col4 and in Col5 after fetching P75 other rows should be unaffected.

来自

DEPOSIT ADD     234567890 (VALUE)(P75)  NNNN    VALUEVARIABLE

DEPOSIT ADD     234567890 (VTG)(SPVTG)  TCM    VTGSPVTG

  1. 只有包含 P75 的记录才能像这样更新.所有选定记录的替换值都相同.

我写的脚本是

$original_file='C:\Path\20200721130155_copy.txt' -header Col1,Col2,Col3,Col4,Col5,| Select Col3,Col4,Col5

(Get-Content $original_file) |ForEach-Object {

    if($_.Col3 -match '(VALUE)(P75)')
    {
    $_ -replace '(VALUE)(P75)', '(VTG)(SPVTG)' `
       -replace 'VALUEVARIABLE', 'VTGSPVTG' `
       -replace 'NNNN', 'TCM' `
    } 
    $_
    }| Set-Content $original_file+'_new.txt' -Force
 

我正在获取具有相同内容的输出文件.文件未更新.

I am getting output file with same content. The file is not getting updated.

请指教.

谢谢

推荐答案

您可以执行以下操作:

$newfile = "{0}_new.txt" -f $original_file
Get-Content $original_file | Foreach-Object {
    if ($_ -match '\(VALUE\)\(P75\)') {
        $_ -replace '\(VALUE\)\(P75\)','(VTG)(SPVTG)' -replace 'VALUEVARIABLE', 'VTGSPVTG' -replace 'NNNN', 'TCM'
    } else {
        $_
    }
} | Set-Content $newfile -Force

由于 -replace 使用正则表达式,您必须反斜杠转义特殊的正则表达式字符,如 ().

Since -replace uses regex, you must backslash escape special regex characters like ( and ).

由于 $_ 是从 Get-Content 读取的当前行,没有 -Raw,你需要输出 $_ 如果你不想做任何改变.如果您确实想替换文本,则 $_ -replace 'regex','text' 将输出带有替换文本的该行.

Since $_ is the current line read from Get-Content without -Raw, you will need to output $_ if you want to make no changes. If you do want to replace text, then $_ -replace 'regex','text' will output that line with the replaced text.

或者,您可以在 switch 语句中应用上述相同的逻辑,这样效率更高:

Alternatively, you can apply the same logic above in a switch statement, which is more efficient:

$newfile = "{0}_new.txt" -f $original_file
$(switch -regex -file $original_file {
    '\(VALUE\)\(P75\)' {
        $_ -replace '\(VALUE\)\(P75\)','(VTG)(SPVTG)' -replace 'VALUEVARIABLE', 'VTGSPVTG' -replace 'NNNN', 'TCM'
    }
    default { $_ }
}) | Set-Content $newfile -Force

这篇关于如何使用powershell脚本替换文本文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆