使用替换语句简化PowerShell脚本

时间:2017-12-21 03:40:26

标签: powershell

我正在尝试在Windows 2012 R2中的50GB文件上运行此脚本,我希望将三个替换语句分成一个而不是三个。此外,重要的是替换按此顺序发生。任何简化此操作并使其高效运行的建议都将非常感激!

$filePath = "D:\FileLocation\file_name.csv"
(Get-Content $filePath | out-string).Replace('"', '""') | Set-Content $filePath
(Get-Content $filePath | out-string).Replace('|~|', '"') | Set-Content $filePath
(Get-Content $filePath | out-string).Replace('|@|', ',') | Set-Content $filePath

3 个答案:

答案 0 :(得分:4)

对于这么大的文件,我建议您逐行(或批量)处理文件,这样可以加快整个过程。

您可以在此处复制True提及的脚本http://community.idera.com/powershell/ask_the_experts/f/learn_powershell-12/18821/how-to-remove-specific-rows-from-csv-files-in-powershell

但不是直接写出$ Line,而是替换

<link type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<nav id="nav" class="clearfix">
  <a href="index.html" id="homelink"><i class="fa fa-home fa-lg" aria-hidden="true"></i> Home</a>
  <div id="navlistcontainer">
    <a href="signup.php"><button class="navbaritem" id="signup">SIGNUP</button></a>
    <a href="login.php"><button class="navbaritem" id="login">LOGIN</button></a>
  </div>
</nav>

<!-- Big Main Page Block -->
<div class="mainBlock">
  <h1>Welcome to site</h1>
  <h3>siteinfo<br> moreinfo
  </h3>
  <div class="buttonsWrapper">
    <form class="mainButtons">
      <input type="submit" value="Basdasds" id="browse">
      <input type="submit" value="Cs" id="create" formaction="debate.php">
    </form>
    <!-- Ends the buttons form -->
  </div>
  <!-- Ends buttons wrapper -->
</div>

小心get-content,因为它会尝试加载整个文件,并在内存不足时变得非常慢。 如果您没有足够的磁盘空间,也要小心。在替换之前,链接的解决方案将复制文件(包含更改)。

答案 1 :(得分:1)

您可以使用-replace operator

$filepath="c:\temp\text.txt"
(Get-Content $filepath) -replace 'test','1' -replace 'text','2' -replace '123','3' |Set-Content $filepath

答案 2 :(得分:0)

您可以将.replace()组合在同一行中。

public function store() { 
    $data = request()->validate('email');
    Subscriber::create($data);
    return redirect()->route('homepage'); 
}
相关问题