Solved

cut and paste line to new text file

Posted on 2007-04-02
12
218 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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