Solved

cut and paste line to new text file

Posted on 2007-03-30
4
219 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
  • 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now