Solved

cut and paste line to new text file

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

690 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