如何使用Windows命令行环境查找和替换文件中的文本? [英] How can you find and replace text in a file using the Windows command-line environment?

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

问题描述

我正在使用Windows命令行环境编写一个批处理文件脚本,并希望用另一个(例如BAR)更改文件中的某些文本(例如FOO)的每一次出现。最简单的方法是什么?任何内置的函数?

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?

推荐答案

如果你是支持.Net 2.0的Windows版本, PowerShell 可从命令行获得.Net的全部功能。还有许多内置的小命令。下面的示例将解决您的问题。我使用命令的全名,有更短的别名,但这给了你Google的一些东西。

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天全站免登陆