• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

cut and paste line to new text file

I need to search for a word in a text file then cut and paste that line and any line after until either another word occurs or end of the file into a new text file.

So if looking for the word Production it would cut that and any line until it hit the word Staging or the end of the file:
blah
blah
blah
Production
blah
blah
blah
Staging
blah
blah
Production
blah

So the new text file would have:
Production
blah
blah
blah
Production
blah

And the old file would have:
blah
blah
blah
Staging
blah
blah
0
whargra
Asked:
whargra
  • 2
  • 2
1 Solution
 
Steve KnightIT ConsultancyCommented:
Try this... untested at the moment, sorry so post back any issues.

Steve

@echo off
set source=c:\yourfile.txt
set first=c:\file1.txt
set second=c:\file2.txt
set lookfor1=Staging
set lookfor2=Production
set currentfile=%first%
for /f "tokens=*" %%a in (%source%) do call :process %%a
goto end

:process
set line1=%*
REM Redirect output to second file now.
if "%line1%"=="%lookfor1%" set currentfile=%second%
REM Ignore anything after second word
if "%line1%"=="%lookfor2%" set currentfile=nul
echo %line1%>>%currentfile%
:end
0
 
whargraAuthor Commented:
It does not cut and paste the entire line so trying to figure out why.
0
 
whargraAuthor Commented:
Data issue. So have that done.

I would like to add one more thing to this, so I will open a new question. Thanks for the help.
0
 
Steve KnightIT ConsultancyCommented:
No problem, not here much at the mo. so someone else will probably pick it up but will keep an eye out.
Steve
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now