相当于 Windows 上的“剪切”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25066360/
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-08 18:38:05  来源:igfitidea点击:

Equivalent to 'cut' on Windows

windowsbatch-file

提问by user3437212

I have a file which has entries as below.

我有一个文件,其中包含如下条目。

/a/b/c/d/e/f
/a/b/c/g/k/l/f/j/h
/a/b/c/i/m/n/p

I need a command in Windows which would remove the '/a/b/c'part from the file.

我需要在 Windows 中使用一个命令来'/a/b/c'从文件中删除该部分。

The output file should look like

输出文件应该看起来像

d/e/f
g/k/l/f/j/h
i/m/n/p

I tried using the forcommand with /as the delimiter, but I couldn't get the expected result. How can I do this?

我尝试使用for命令 with/作为分隔符,但无法得到预期的结果。我怎样才能做到这一点?

回答by MC ND

@echo off

(for /f "tokens=3,* delims=/" %%a in (input.txt) do echo %%b) > output.txt

And the "trick" is to ask for the third token and the rest of the line.

而“技巧”是要求第三个标记和该行的其余部分。

To use it directly from the command line:

要直接从命令行使用它:

(for /f "tokens=3,* delims=/" %a in (input.txt) do @echo %b) > output.txt

The escaped percent signs are simplified and, as from command line the echo offis not enabled by default, the @before echois needed.

转义的百分号被简化,并且从命令行echo off默认情况下不启用,需要@before echo

回答by Bill_Stewart

PowerShell:

电源外壳:

get-content test.txt | foreach-object {
  $_ -replace '/a/b/c/',''
} | out-file test2.txt