【问题标题】:How to replace / with \ using shell command in text file如何在文本文件中使用 shell 命令将 / 替换为 \
【发布时间】:2020-06-27 23:14:27
【问题描述】:

我有一个文本文件 File.txt,内容如下

Application_Servers\Base\Baseinstall\BGInfo
Application_Servers\Base\Baseinstall\CLRRSServerManager
Application_Servers\Base\Baseinstall\PCSUtils
Application_Servers\Base\Baseinstall\PostOSLoad
Application_Servers\Base\Baseinstall\Registration
Application_Servers\Base\Baseinstall\WMI
Application_Servers\Base\Baseinstall\x64\RSAutoExNt\v1.2
Application_Servers\Base\w2k16update\EveryTime
RS Batch\RSQueueClientLib\V_1_0
RS Batch\RSQueueClientLib\V_2_2
Utilities\Clrstuff\clrrs\Bin

我想用/替换\,我该如何实现呢?请给我shell命令

【问题讨论】:

  • 你自己试过什么,什么没用?
  • 这取决于你走哪条路。如果你有你展示的内容并想用'/'替换'\'并且你在Unix/Linux机器上,你想要tr '\\' '/'(例如tr '\\' '/' < file > newfile)..如果你不在Unix/ Linux 机器,那么为什么有 POSIX shell 标签 [shell][sh] 对 Windows 没有应用(WSL 除外)。

标签: powershell shell sh powershell-2.0 powershell-3.0


【解决方案1】:

gc = 获取内容 sc = 设置内容

获取内容....

Get-Content cmdlet 在该位置获取项目的内容 由路径指定,例如文件中的文本或文件的内容 功能。对于文件,一次读取一行内容,然后 返回一个对象集合,每个对象代表一行 内容。 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-7

设置内容....

Set-Content 是一个字符串处理 cmdlet,用于写入新内容或 替换文件中的内容。 Set-Content 替换现有的 内容,与附加内容的 Add-Content cmdlet 不同 到一个文件。要将内容发送到 Set-Content,您可以使用 Value 命令行上的参数或通过管道发送内容。 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content?view=powershell-7

-replace() .....

替换字符串中的字符

代码.... (gc "C:\folder\File.txt") -replace '\\','/' | sc "C:\folder\File.txt"

如果您还没有尝试自己编写脚本并被卡住,请不要再询问有关 stackoverflow 的任何问题。当您遇到困难时,我们在这里。就目前而言,您甚至还没有开始或尝试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2011-02-01
    • 2019-10-29
    • 2020-02-14
    • 2021-11-14
    • 2020-08-03
    • 1970-01-01
    相关资源
    最近更新 更多