在大文件中查找和替换 [英] Find and Replace in a Large File
问题描述
我想在一个大的 xml 文件中找到一段文本,并想用其他一些文本替换.文件大小约为(50GB).我想在命令行中执行此操作.我正在查看 Powershell,想知道它是否可以处理大尺寸.另外我想知道在powershell中转义关键运算符的语法.我是 PowerShell 新手
I want to find a piece of text in a large xml file and want to replace with some other text. The size of the file is around ( 50GB). I want to do this in command line. I am looking at Powershell and want to know if it can handle the large size. Also I would like to the know the syntax for escaping the key operators in powershell. I am a PowerShell newbie
目前我正在尝试这样的东西,但它不喜欢它
Currently I am trying something like this but it does not like it
Get-Content C:\File1.xml | Foreach-Object {$_ -replace "xmlns:xsi=\"http:\/\/www\.w3\.org\/2001\/XMLSchema-instance\"", ""} | Set-Content C:\File1.xml
我要替换的文本是 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 带有空字符串 "".
The text I want to replace is xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" with empty string "".
问题
- powerShell 可以处理大文件
- 我如何调用命令行中的 powershell 脚本
- 转义键的语法PowerShell 中的运算符和列表PowerShell 中的关键运算符.
- 我不希望替换发生在内存和更喜欢流假设这不会使服务器它的膝盖.
- 我可以采取其他任何方法吗(不同的工具/策略?)
谢谢
推荐答案
我有类似的需求(并且类似缺乏 powershell 经验),但从本页上的其他答案中拼凑出一个完整的答案,并进行了更多研究.
I had a similar need (and similar lack of powershell experience) but cobbled together a complete answer from the other answers on this page plus a bit more research.
我也想避免正则表达式处理,因为我也不需要它——只是一个简单的字符串替换——但是在一个大文件上,所以我不想把它加载到内存中.
I also wanted to avoid the regex processing, since I didn't need it either -- just a simple string replace -- but on a large file, so I didn't want it loaded into memory.
这是我使用的命令(添加换行符以提高可读性):
Here's the command I used (adding linebreaks for readability):
Get-Content sourcefile.txt
| Foreach-Object {$_.Replace('http://example.com', 'http://another.example.com')}
| Set-Content result.txt
完美运行!从来没有占用太多内存(很明显它没有将整个文件加载到内存中),只是慢吞吞地坚持了几分钟然后就完成了.
Worked perfectly! Never sucked up much memory (it very obviously didn't load the whole file into memory), and just chugged along for a few minutes then finished.
这篇关于在大文件中查找和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!