creating a "find" output with ONLY the lines you want!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
papageek
Posts: 3
Joined: 03 Sep 2012 09:46

creating a "find" output with ONLY the lines you want!

#1 Post by papageek » 03 Sep 2012 10:01

I'm trying to use find to strip some lines out of an existing file using the /v options. When I do the output contains the line ------ filename, preceded by a blank line. When I pipe the output to kill the ----- line I still get the blank line in my output.

Here is the output to a dos prompt that illustrates the problem:

Code: Select all

C:\Documents and Settings\Sandy>echo abc > first

C:\Documents and Settings\Sandy>echo 123 >> first

C:\Documents and Settings\Sandy>find /v "abc" first > second

C:\Documents and Settings\Sandy>find /v "abc" first | find /v "-----" > third

C:\Documents and Settings\Sandy>type first
abc
123

C:\Documents and Settings\Sandy>type second

---------- FIRST
123

C:\Documents and Settings\Sandy>type third

123

C:\Documents and Settings\Sandy>


The first file contains both lines. The second file does not contain the abc line, but does contain the filename preceded by a lot of hyphens. The third file has stripped off the filename, but still contain the extra blank line.
Is there any way to get rid of this extra blank line?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: creating a "find" output with ONLY the lines you want!

#2 Post by abc0502 » 03 Sep 2012 11:14

If you used a for command and the skip switch, that should help

Code: Select all

for /f "skip=2" %a in ('find /v "abc" "1.txt"') Do echo %a>>m.txt

Using the above command in a cmd window will put the result in a file m.txt without the empty line

papageek
Posts: 3
Joined: 03 Sep 2012 09:46

Re: creating a "find" output with ONLY the lines you want!

#3 Post by papageek » 03 Sep 2012 12:11

Thanks for the first hint ABC. It got rid of the first empty line, but also truncated the lines. Here is an example of the actual BAT file that I’m using, xx.bat (with a couple extra type command to show the input and output):

Code: Select all

@echo off
type server\myssi_subnav.inc
del newfile.inc
for /f "skip=2" %%a in ('find /v "myvideo" "server\myssi_subnav.inc"') Do echo %%a >> newfile.inc
echo myvideo ================ deleted
type newfile.inc


And here is the output that it produces:

Code: Select all

E:\Documents\BitWare\BitSites\VariableHTML>xx
<div id="subnav">
<ul>
<li><a href=parentchild.htm><span>Parent Child</span></a></li>
<li><a href=grandparent.htm><span>Grandparent</span></a></li>
<li><a href=archive.htm><span>Archive</span></a></li>
<li><a href=commands.htm><span>Commands</span></a></li>
<li><a href=myvideo.htm><span>Commands</span></a></li>
<li><a href=myexample.htm><span>Example</span></a></li>
<li><a href=features.htm><span>Features</span></a></li>
</ul>
</div>
myvideo ================ deleted
"<div"
"<ul>"
"<li><a"
"<li><a"
"<li><a"
"<li><a"
"<li><a"
"<li><a"
"</ul>"
"</div>"
E:\Documents\BitWare\BitSites\VariableHTML>


I had to double the percent signs because they are used inside a BAT file, and then the “echo” result was truncated at the first space for some reason.
Any idea on what is causing this?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: creating a "find" output with ONLY the lines you want!

#4 Post by abc0502 » 03 Sep 2012 12:51

sorry it's my bad, you should change the for command and add "tokens=*" after the "skip" so your batch should be like this:

Code: Select all

@echo off
type server\myssi_subnav.inc
del newfile.inc
for /f "skip=2 tokens=*" %%a in ('find /v "myvideo" "server\myssi_subnav.inc"') Do echo %%a >> newfile.inc
echo myvideo ================ deleted
type newfile.inc


let's say we have this sentence "I know how to write a batch file", the token could be considered as the words such as "know" or "I" or batch" and so IF the delims which could be considered as a separator was set as a "space" between the words, and the "*" mean that we take "all" tokens, I hope that was clear enough to understand :?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: creating a "find" output with ONLY the lines you want!

#5 Post by Squashman » 03 Sep 2012 12:55

As always if you don't provide an accurate example of what your input is your output will not be accurate either.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: creating a "find" output with ONLY the lines you want!

#6 Post by foxidrive » 03 Sep 2012 16:48

papageek wrote: Here is an example of the actual BAT file that I’m using, xx.bat (with a couple extra type command to show the input and output):


You will find that when you redirect the input into FIND.EXE that it is used as a filter and the header is eliminated.

Code: Select all

find /v "myvideo" < "server\myssi_subnav.inc"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: creating a "find" output with ONLY the lines you want!

#7 Post by foxidrive » 03 Sep 2012 17:05

An example:

Code: Select all

@echo off
echo abc > first
echo 123 >> first
echo ...
find /v "abc" first
echo ...
find /v "abc" <first
echo ...


...

---------- FIRST
123
...
123
...
Press any key to continue . . .

papageek
Posts: 3
Joined: 03 Sep 2012 09:46

Re: creating a "find" output with ONLY the lines you want!

#8 Post by papageek » 03 Sep 2012 17:42

Squashman you are absolutely correct! I tried to “simplify” the problem and wound up making it more difficult to understand what I really needed.

Here is the new code that works even better with foxidrive’s suggestion

Code: Select all

@echo off
echo * * * * this is the original file: * * * *
type server\myssi_subnav.inc
del newfile.inc
for /f "skip=2 tokens=*" %%a in ('find /v "myvideo" "server\myssi_subnav.inc"') Do echo %%a >> newfile.inc
echo * * * *  This is the file as created with abc0502's suggestion * * * *
type newfile.inc
find /v "myvideo" < "server\myssi_subnav.inc" > anotherfile.inc
echo * * * *  This is the file as created with foxidrive's suggestion * * * *
type anotherfile.inc
echo * * * * I want to thank all who commented here,  * * * *
echo * * * * but foxidrive's suggestion looks cleaner in the batch file * * * *

Output

Code: Select all

E:\Documents\BitWare\BitSites\VariableHTML>xx
* * * * this is the original file: * * * *
<div id="subnav">
<ul>
<li><a href=parentchild.htm><span>Parent Child</span></a></li>
<li><a href=grandparent.htm><span>Grandparent</span></a></li>
<li><a href=archive.htm><span>Archive</span></a></li>
<li><a href=commands.htm><span>Commands</span></a></li>
<li><a href=myvideo.htm><span>Commands</span></a></li>
<li><a href=myexample.htm><span>Example</span></a></li>
<li><a href=features.htm><span>Features</span></a></li>
</ul>
</div>
* * * *  This is the file as created with abc0502's suggestion * * * *
<div id="subnav">
<ul>
<li><a href=parentchild.htm><span>Parent Child</span></a></li>
<li><a href=grandparent.htm><span>Grandparent</span></a></li>
<li><a href=archive.htm><span>Archive</span></a></li>
<li><a href=commands.htm><span>Commands</span></a></li>
<li><a href=myexample.htm><span>Example</span></a></li>
<li><a href=features.htm><span>Features</span></a></li>
</ul>
</div>
* * * *  This is the file as created with foxidrive's suggestion * * * *
<div id="subnav">
<ul>
<li><a href=parentchild.htm><span>Parent Child</span></a></li>
<li><a href=grandparent.htm><span>Grandparent</span></a></li>
<li><a href=archive.htm><span>Archive</span></a></li>
<li><a href=commands.htm><span>Commands</span></a></li>
<li><a href=myexample.htm><span>Example</span></a></li>
<li><a href=features.htm><span>Features</span></a></li>
</ul>
</div>
* * * * I want to thank all who commented here,  * * * *
* * * * but foxidrive's suggestion looks cleaner in the batch file * * * *

E:\Documents\BitWare\BitSites\VariableHTML>

If anyone is a website developer, I have some very interesting code that I will share that makes website development a little easier. The MySSI project mentioned in the post is basically a server side emulator.

The question I asked is for "resetting" a page I am creating for a video demo of the process. I need to be able to create the page for the video, then delete everything, all links in the included file, etc., and do it again till I get the video right.

Post Reply