在 Powershell 中替换多行 [英] Replace multiple lines in Powershell

查看:61
本文介绍了在 Powershell 中替换多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此脚本替换文件中的文本 (Powershell V.1)

I'm using this script to replace a text in a file (Powershell V.1)

powershell -Command "(gc 'file.jnlp') -replace 'ruta_logo','logo/logo.png' 
| out-file 'file2.jnlp'"

但是如果我想替换多行,我该如何制作脚本?我在想这样的事情:

But if I want to replace multiples lines how can I make the script? I was thinking something like this:

powershell -Command "(gc 'file.jnlp') -replace 'ruta_logo','logo/logo.png' 
-replace 'ruta_xsl','logo/xsl.xsl' `
-replace 'url_msg','url+msg'`
-replace 'url_forest','url+forest'`
-replace 'nombre_entidad','Nombre'`
-replace 'url_word','Word.exe' | out-file 'file2.jnlp'"

但是没有用.所以我使用多个批处理文件来替换一行.谢谢

But is not working. So I'm using multiples batch files to replace a line. Thanks

推荐答案

我决定发布这个 Batch 文件解决方案的原因如下:

I decided to post this Batch file solution for the following reasons:

  • 原始问题batch-file 标签.
  • 最后一个 OP 的评论表明他想从 .bat 文件中运行代码.
  • 这些要点是由我解释",因为解决此问题的最佳方法是将所有代码保存在一个 Batch .bat 文件中.
  • The original question had the batch-file tag.
  • The last OP's comment indicate that he wants to run the code from a .bat file.
  • These points are "interpreted by me" as the best solution for this problem is to keep all the code in a single Batch .bat file.

.

@if (@CodeSection == @Batch) @then

@echo off
cscript //nologo //E:JScript "%~F0" < file.jnlp > file2.jnlp
goto :EOF

@end

// JScript section

WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(
        /(ruta_logo)|(ruta_xsl)|(url_msg)|(url_forest)|(nombre_entidad)|(url_word)/g,
function (o,A,B,C,D,E,F)
{return ["logo/logo.png","logo/xsl.xsl","url+msg","url+forest","Nombre","Word.exe"]
        [[].concat(A,B,C,D,E,F).join().indexOf(o)] }));

作为一个额外的好处,这个解决方案应该比 PowerShell 运行得更快(恕我直言,它也更简单).

As an additional benefit, this solution should run faster than the PowerShell one (and IMHO it is simpler also).

这篇关于在 Powershell 中替换多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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