Using FIND and IF EXIST to delete a specific phrase from a text file
I am trying to trim a text file, although I have used the following command with no luck:
FIND "word1" C:\Users\Use开发者_开发技巧rname\Desktop\test.txt | IF EXIST "word1" (DEL "word1")
The syntax is incorrect, I have tried many different combination with no luck.
If you are trying to remove specific text from a file, you can use sed (there are versions available for Windows such as this one). For example, to remove all instances of "word1":
sed -e "s/word1//g" inputfile > outputfile
Or if you want to only remove "word1" when it is not embedded in other text:
sed -e "s/\bword1\b//g" inputfile > outputfile
The second one uses \b
to indicate word boundaries. Note that in a Windows command prompt, you need to enclose the sed script in double quotes.
精彩评论