你怎么能找到并使用Windows命令行环境的文件替换文本? [英] How can you find and replace text in a file using the Windows command-line environment?

查看:454
本文介绍了你怎么能找到并使用Windows命令行环境的文件替换文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写信使用Windows命令行环境的批处理文件脚本,并希望与另一个改变的文件(如FOO)一些文本的每一次出现(如BAR)。什么是做到这一点的最简单的方法?任何内置的功能?

I am writing a batch file script using Windows command-line environment and want to change each occurrence of some text in a file (ex. "FOO") with another (ex. "BAR"). What is the simplest way to do that? Any built in functions?

推荐答案

如果您使用的是Windows版本,我会取代你的shell。 PowerShell的给你的.NET命令行的全部功能。有内置的以及许多命令开关。下面的例子将解决你的问题。我使用的命令的全名,也有短的别名,但这个给你的东西,以谷歌为。

If you are on Windows version that supports .Net 2.0, I would replace your shell. PowerShell gives you the full power of .Net from the command line. There are many commandlets built in as well. The example below will solve your question. I'm using the full names of the commands, there are shorter aliases, but this gives you something to Google for.

(Get-Content test.txt) | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt

这篇关于你怎么能找到并使用Windows命令行环境的文件替换文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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