Solved

cut and paste line to new text file

Posted on 2007-04-02
12
213 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

10 Experts available now in Live!

Get 1:1 Help Now