Link to home
Start Free TrialLog in
Avatar of whargra
whargra

asked on

cut and paste line to new text file

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
Avatar of Shift-3
Shift-3
Flag of United States of America image

So you want all lines containing " adj-it" moved into a second file?
Avatar of whargra
whargra

ASKER

That is correct.
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
Avatar of Steve Knight
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
Avatar of whargra

ASKER

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?
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
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of whargra

ASKER

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.
Avatar of whargra

ASKER

Okay the /b command creates an empty file.

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

Also added the /I.
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
Avatar of whargra

ASKER

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.
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