如何从文本文件中查找单词并使用批处理文件复制将下一个单词粘贴到该行上 [英] how to find a word from a text file and copy paste the next word on that line using batch file
问题描述
我想从文本文件中找到一个单词,然后使用批处理文件将下一个单词复制粘贴到该行上.
I want to find a word from a text file and copy paste the next word on that line using batch file.
例如:我叫xyz.我喜欢科学.我喜欢打排球.
For Ex: my name is xyz. i like science. i like to play volleyball.
这是文本文件,我想找到单词"play"并在其中显示下一个单词排球".
This is the text file and I want to find the word "play" and display the next word to it "volleyball".
如何为它编写Windows批处理文件?
How can I write windows batch file for it?
推荐答案
您的问题的字面意思是使用记事本和键盘"
the literal answer to your question would be "by using Notepad and your keyboard"
您想阅读的答案:
使用子字符串替换从一开始就删除所有内容,直到(包括)您的搜索词为止. (请参见set /?
)
使用for
循环从字符串的其余部分获取第一个单词. (请参见for /?
)
The answer, you want to read:
use substring replacement to remove everything from the start until (including) your search word. (see set /?
)
Use a for
loop to get the first word from the rest of the string. (see for /?
)
@echo off
echo my name is xyz. i like science. i like to play volleyball and drink whiskey. >abc.txt
<abc.txt set /p "text="
for /f "delims=.,;! " %%a in ('echo %text:* play =%') do set result=%%a
echo the found word is: %result%
REM to write the found word into a new file, use redirection:
echo %result% >xyz.txt
编辑以匹配what needs to do with multiple lines ?
:
@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%z in (abc.txt) do (
set "text=%%z"
for /f "delims=.,;! " %%a in ('echo "!text:* play =!"') do set "result=%%~a"
echo !result! >>xyz.txt
)
需要一些小的更改; 延迟扩展,另一个for /f loop
环绕并用>>
附加到文件.
Some small changes needed; delayed expansion, another for /f loop
wraped around and appending to file with >>
.
注意:请不要使这个问题无休止地变化.遇到其他问题时,请提出新问题
Note: Please don't make this a endless changing question. When you have another problem, please ask a new question
这篇关于如何从文本文件中查找单词并使用批处理文件复制将下一个单词粘贴到该行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!