Solved

cut and paste line to new text file

Posted on 2007-04-02
12
214 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
 
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
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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A short film showing how OnPage and Connectwise integration works.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 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

17 Experts available now in Live!

Get 1:1 Help Now