powershell - Powershell - 将文本放入变量中,使用文本替换另一个文本文件中的文本
问题描述
Powershell - 将文本放入变量中,使用文本替换另一个文本文件中的文本
任何人都知道我怎样才能让这个代码工作?
#The following fetches the first word of the path of the Public folder to use to modify the string in the next command.
$PFNameString = Get-Content("H:\Temp\PublicFolder-powershell\Word_1_Export.csv")
$Option = [System.StringSplitOptions]::RemoveEmptyEntries
$Separator = "\"
$arr = $PFNameString.Split($Separator,$Option)
$Word = $arr[0] #This is the one word i want to use as replacement text to search for in text file loaded next.
$b = Get-Content -Path "H:\Temp\PublicFolder-powershell\PFPerm.02_YandU_correct_AccessRights.csv"
@(ForEach ($a in $b) {$a.Replace('",$Word', '"$Word')}) > "H:\Temp\PublicFolder-powershell\PFPerm.03_Qot_And_D_correct_AccessRights.csv"
我知道如何让它替换,但我希望它使用我从第一个文本文件导入的数组中提取的 $Word 变量中的单词,作为在第二个文本文件中搜索和替换的“通配符” .
在这种情况下,单词是“Distrikt”
我希望脚本将 '",Distrikt' 替换为 '"Distrikt'
并且如果变量文本改变了,脚本仍然会使用变量文本作为通配符进行搜索。
谢谢
编辑:之后工作脚本的结果:
########## This script first opens the file Word_1_Export.csv and split it into array segments.
######### Then the first word in the array is stored in the variable $Word to make the search function more selective
######## This script opens PFPerm.02_YandU_correct_AccessRights.csv and saves back to PFPerm.03_Qot_And_D_correct_AccessRights.csv after processing.
####### This code replace the first ",$Word in the line, with "$Word so the name will be valid for later processing.
###### This code replace the first ",$WORD" in the line with "$WORD" without comma.
##### The goal with this script is to replace the initial COMMA of the line, and saves back to the new file that can be later used.
#The following fetches the first word of the path of the Public folder to use to modify the string in the next command.
$PFNameString = Get-Content("H:\Temp\PublicFolder-powershell\Word_1_Export.csv") #Get text
$Option = [System.StringSplitOptions]::RemoveEmptyEntries
$Separator = "\" #Sign to use as separator of array segments
$arr = $PFNameString.Split($Separator,$Option) #Splits to array segments based on "\"
$Word = $arr[0] #Fetches the first array segment and stores it into the variable $Word
$b = Get-Content -Path "H:\Temp\PublicFolder-powershell\PFPerm.02_YandU_correct_AccessRights.csv" #Loads in the next text file.
#Special quotes under here is required for the Variable to be possible to access ("`",$Word", "`"$Word")}) - The special single quote here instead of "'" is needed.
#If you only had a static word to lookup, you could specify it with ('",StaticWord', '"StaticWord')}) and do the same, but it would not be dynamic as with this below line of code.
@(ForEach ($a in $b) {$a.Replace("`",$Word", "`"$Word")}) > "H:\Temp\PublicFolder-powershell\PFPerm.03_Qot_And_D_correct_AccessRights.csv"
Start-Sleep -Seconds 0.3 #grants time for script to save txt-files back.
解决方案
$a.Replace('",$Word', '"$Word')
这里的单引号将返回一个字符串文字。你会从字面上得到这些引号之间的字符。
如果你想表达$Word
你需要另一个攻击的变量。尝试:$a.Replace("`",$Word", "`"$Word")
(注意反引号以转义要替换的双引号。)
推荐阅读
- opengl - 如何保持 Opengl Scatter Instances 大小不变?
- c# - C# 仅将 DateTime 字符串解析为时间
- javascript - Javascript中的HTML div到PDF - 错误文本不可选择
- java - 使用 selenium java 我想自动化一个调查页面,有 11 个问题。每个问题都有多个答案
- python - 停止 RecycleView 对项目进行排序
- discord.js - Discord.js 为不和谐服务器翻译机器人
- flutter - 当我单击显示此错误的后退按钮时?
- javascript - 工具提示 悬停在另一个项目上
- css - 屏幕宽度查询与 SCSS 和 Create React App 一起工作不正确
- c# - 添加用户控件而不刷新数据