Find and Replace in a Large File

前端 未结 5 1838
时光取名叫无心
时光取名叫无心 2021-02-09 15:52

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 look

5条回答
  •  猫巷女王i
    2021-02-09 16:24

    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.

提交回复
热议问题