Batch file to Copy particular lines from one text file to another text file

心不动则不痛 提交于 2019-12-06 13:31:21

You could use the /g: option to findstr, basically do

findstr /g:pattern.txt %InFile% > %OutFile%

where pattern.txt is (in your example)

I want
Also this

This assumes you can write findstr regular expressions for all the lines you want to copy.

If you use findstr with multiple files (wildcard) you will get the file name prepended. To get round this, use del out.txt for %F in (*.txt); do findstr /g:pattern.txt %F >> out.txt Note that you should put the source files in a different directory to the pattern and output files (or use a different extension) otherwise the *.txt wildcard will pick those files up, too.

You can also use sed for this purpose. Certain lines (in this case 2 and 3) are copied as follows:

sed -n -e 2p -e 3p input.txt > output.txt

If you wish to copy a segment of lines (all lines from line 2 to line 10) then you may use:

sed -n -e 2,10p input.txt > output.txt

Or you can also use combinations of certain lines and segments:

sed -n -e 2,10p -e 15p -e 27p input.txt > output.txt
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!