Replacing characters in a text file with a batch file

前端 未结 7 389
一个人的身影
一个人的身影 2021-01-18 08:03

Is there a way to replace some characters in a text file with a batch file?

I didn\'t find any command to do that.

相关标签:
7条回答
  • 2021-01-18 08:43

    Use sed or nothing:

    sed -i 's/FROM/TO/g' filename.txt
    

    sed can be download here, for various platforms.

    0 讨论(0)
  • 2021-01-18 08:48

    Even though this question is quite old, future reader may be interested to know that if you want a small exe with no dependencies for just replacing text I recommend using FART.

    0 讨论(0)
  • 2021-01-18 08:49

    You can use DOS port of Unix command line utility "tr".

    A free DOS port can be found here, one of the ones I like (there are many different ones) are "UXUTL — Comprehensive collection of command line Unix utilities for DOS."

    The benefit is that you get a WHOLE BUNCh of incredibly useful commands, not just tr.

    0 讨论(0)
  • 2021-01-18 08:51

    You have to use WIN32 SED and see the official gnu sed page for explanation. It is really powerful :

    > sed "s/WORD_FROM/WORD_TO/" file_name > changed.file.name
    
    0 讨论(0)
  • 2021-01-18 08:59

    The quick answer is "No, not with basic windows utilities"

    But as the other answers suggested, there are lots of unix ports out there that do what you want. Take alook at gnuwin32 packages.

    EDIT:

    Okay, I revise my strict "No". There might be a way of doing it, depending on the complexity of your task and your OS. When using windows 2000 and above, cmd provides command extensions that you can use.

    The basic idea is to use a FOR loop to go through each line of an input file and then to use string substitution provided by the SET command to replace your characters.

    I have no solution at hand but you might try on your own, using infromation from this quite cool site. Look here for the FOR loop syntax and here for the string substitution.

    0 讨论(0)
  • 2021-01-18 09:08

    You can search for a command line program like GREP you can call from a batch that will take a replace string and a file and do the swap for you.

    0 讨论(0)
提交回复
热议问题