使用powershell

时间:2018-08-21 11:44:52

标签: regex powershell

情况:

我必须从文件中删除特殊字符和空格。句子引号之间的空格不应删除。仅需要删除字符串开头和结尾的空格。

输入:

我将输入内容保持为{}

之间的文本格式
{" fakldjfqllewfllwefklwdkf wefw "  ,XCSLDKVNADUFHef328E498234750139458YefA88787     ,"&*%&*^*&^()*)(*(*T&*UKLKJGYTDUJ:"                ,LMKCXXBJIUY&*(efewfqerfqerrferrferfeffgfadfgafegkEJFGI} 

我试图解决的Powershellcode:

$objFile1 = Get-Content "D:\my-training\powershell\Sample_file.csv"

foreach($line in $objFile1)
{
  $String = "$line"
  $value= $String -replace '[^\p{L}\p{Nd}/"/,]', ''
  Add-Content -path "D:\my-training\powershell\result.csv" -value $value 
}

输出:

我将输出保持为文本格式,{}

{"fakldjfqllewfllwefklwdkfwefw",XCSLDKVNADUFHef328E498234750139458YefA88787,"TUKLKJGYTDUJ",LMKCXXBJIUYefewfqerfqerrferrferfeffgfadfgafegkEJFGI}

预期输出:

{"fakldjfqllewfllwefklwdkf  wefw",XCSLDKVNADUFHef328E498234750139458YefA88787,"TUKLKJGYTDUJ",LMKCXXBJIUYefewfqerfqerrferrferfeffgfadfgafegkEJFGI}

问题:

问题在于,代码还删除了字符串本身中的空格,我只希望剪切开头和结尾的空格。

1 个答案:

答案 0 :(得分:0)

嗯,这肯定是一个看起来很奇怪的字符串。 无论如何,这可以为您解决问题:

const cleanUndefined = object => JSON.parse(JSON.stringify(object));
const testWithoutUndefined = cleanUndefined(test)

输出

{"fakldjfqllewfllwefklwdkf wefw",XCSLD;KVNADUFHef328E498234750139458YefA88787,"&*%&*^*&^()*)(*(*T&*UKLKJGYTDUJ:",LMKCXXBJIUY&*(efewfqerfqerrferrferfeffgfadfgafegkEJFGI}