Solved

cut and paste line to new text file

Posted on 2007-04-02
12
217 Views
Last Modified: 2010-04-16
I want to cut and paste a line of text to a new text file. The line will always start with " adj-it".
Note there is a space in front of adj-it.

w23ed
 adj-it
3er4w
vfter
 adj-it
zxcvbbb
4567864
sdfujtee
 adj-it
0
Comment
Question by:whargra
  • 5
  • 5
  • 2
12 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 18837072
So you want all lines containing " adj-it" moved into a second file?
0
 

Author Comment

by:whargra
ID: 18837391
That is correct.
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 18837857
The script below should do what you require, though it may choke on special characters such as ampersands.

Once you have tested it sufficiently, remove the REM from the line with the "move" command.


@echo off
setlocal

for /F "tokens=* delims=" %%G in (file1.txt) do call :_process "%%G"

REM move /Y file1a.txt file1.txt

goto :_end

:_process
set line=%~1
if /I "%line:~0,7%" EQU " adj-it" (
 echo %line% >> file2.txt
 goto :eof
)
echo %line% >> file1a.txt
goto :eof

:_end
endlocal
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 18839634
You could also do this using the findstr commands:

findstr /b " adj-it" file.txt >filea.txt
findstr /b /v " adj-it" file.txt >fileb.txt

Steve
0
 

Author Comment

by:whargra
ID: 18849945
shift-3, your script gives me an "Agency was unexpected at this time." message.
Agency is a common word but not part of adj lines. I could not find anything that clued in on what this was.

Steve I tried your cmd as well but it actually just copies the entire file. I am wondering if there is another command to add that would push out that line?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18850340
I imagine you may have characters such as & > < | in the lines.  If you do then anything after that will be treated as part of the command line when using a for loop to process.   The way I suggested with using find or findstr won't suffer from that and can filter out certain text etc.

What do you want out.  The second findstr with the /v should return everything except lines starting with adj-it and the first should return all the lines that do start with it.  A couple of renames can leave you with whichever file called the original name if needed.

If you don't need the original file modified to remove the lines then just the first option should do.

Can you try the findstr commands on your data and post the results please.  If the first findstr is not returning any data perhaps the first bit of the line isn't exactly  " adj-it".


Steve
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 18850389
It would appear that findstr doesn't treat the space at the start as part of the search, try this: works for me on your test data:

findstr /b "adj-it" file.txt > filea.txt
findstr /v /b "adj-it" file.txt > fileb.txt

You can add /I if you want it checked regardless of case.

hth

Steve
0
 

Author Comment

by:whargra
ID: 18850666
I did check that the lines did not have & and looking there are chars such as : and - so that may be it.

I will try your suggestion of dropping the space shortly now.
0
 

Author Comment

by:whargra
ID: 18850727
Okay the /b command creates an empty file.

The /b /v command creates a copy of the original.

Also added the /I.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18850751
Can you try it with the text file you posted above and the exact command lines I have entered above please as they work for me on the data presented.  Perhaps what you have in the file isn't a space character at the beginning for instance.

If you take out the /b it will find the "adj-it" text anywhere on the line.  If that works then I suspect there is something else on the line before adj-it.  Is the adj-it text likely to appear elsewhere anyway?

Steve
0
 

Author Comment

by:whargra
ID: 18850773
I think it is the space before the text. I removed it from a few lines to test and those did work properly. I also checked hex to be sure this was not some weird character but it is a space.

I am going to just do a find and replace to dump it and I think it will be good to go.

Thanks.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18850978
Odd.  While findstr works for me both with and without the /b (beginning of line) you could also use find instead of findstr.  Only reason I suggested findstr is it can check the beginning of the line as opposed to anywhere on the line.

Anyway good luck with it and post back any issues

Steve
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
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 …

773 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