Solved

cut and paste line to new text file

Posted on 2007-03-30
4
224 Views
Last Modified: 2010-04-16
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
Comment
Question by:whargra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 18823902
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
 

Author Comment

by:whargra
ID: 18835917
It does not cut and paste the entire line so trying to figure out why.
0
 

Author Comment

by:whargra
ID: 18836792
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18839612
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question