简体   繁体   中英

How to write a batch file to find a string in a text file and push it into a new text file

i want to write a batch file to find a particular string in a file. If the string is found then i want to redirect the whole line which contains the string to another file. for ex:

suppose a file myfile.txt contains the following text

abcwerthfdh
qwerewtretywr
weqreqwrtabcwerwe
wqerweqabcqwewq

when i launch the batch file giving myfile.txt and abc as command line arguments, then the output should be into a file called newfile.txt and it should contain only the lines which contain the text "abc". if i run this code again it should append to newfile.txt and not delete existing contents. in this case it should push the lines 1, 3 and 4 into newfile.txt

@echo off&setlocal
for /f "delims=" %%a in ('findstr "%~2" "%~1"') do (echo(%%a)>>newfile.txt
type  myfile.txt | findstr /n "abc" >>  newfile.txt 

???

你可以使用FINDSTR

FINDSTR abc myfile.txt >> newfile.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM