bash 使用powershell将文件中的路径从斜杠更改为反斜杠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17176520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:43:34  来源:igfitidea点击:

Change paths from slash to backslash in a file with powershell

regexbashpowershell

提问by mles

I have a config file originating from a unix environment, where the basepath is replaced with the basepath in windows:

我有一个源自 unix 环境的配置文件,其中 basepath 替换为 windows 中的 basepath:

inputdir = D:\etl-i_win_4/input/000/
inputdir = D:\etl-i_win_4/input/001/
inputdir = D:\etl-i_win_4/input/002/
inputdir = D:\etl-i_win_4/input/003/
inputdir = D:\etl-i_win_4/input/004/
inputdir = D:\etl-i_win_4/input/005/
inputdir = D:\etl-i_win_4/input/006/
inputdir = D:\etl-i_win_4/input/007/
inputdir = D:\etl-i_win_4/input/008/
inputdir = D:\etl-i_win_4/input/009/

movepostcmd = D:\etl-i_win_4/divider/bin/os-independant/divider.postprocessing

ctrldir = D:\etl-i_win_4/divider/applogs/

lockglob = /opt/netmind/test/etl-festo/kette/divider/applogs/dividerglob.lock

I need proper windows paths with backslashes. How would a function look like, that reads the config file, identifies the lines with windows paths, and replaces all /with \? Note that the last line with a unix path should be ignored.

我需要带有反斜杠的正确 Windows 路径。读取配置文件、使用 Windows 路径标识行并将所有行替换/\? 请注意,应忽略带有 unix 路径的最后一行。

采纳答案by JPBlanc

you can try thid to replace all path:

您可以尝试使用 thid 替换所有路径:

Get-Content "C:\temp\config.txt" | % {$_ -replace '/','\'} | set-content "C:\temp\config Bis.txt"

Just for Windows path accordind to the fact each line containing a Windows path match "A_LETTER:\" patern ... not so good, but it can do the job :

仅适用于 Windows 路径,根据事实,每行包含一个 Windows 路径匹配 "A_LETTER:\" 模式......不太好,但它可以完成这项工作:

Get-Content "C:\temp\path.txt" | % {if ($_ -match "[A-Z]:\"){$_ -replace '/','\'}else {$_}} | set-content "C:\temp\path Bis.txt"

回答by Nick F

You could also try using .Net Path class:

您也可以尝试使用 .Net Path 类:

Get-Content "C:\temp\config.txt" | % { [System.Io.Path]::GetFullPath($_) }