Don't copy 3 lines to text file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Gamer95
Posts: 32
Joined: 26 Sep 2015 03:05

Don't copy 3 lines to text file?

#1 Post by Gamer95 » 26 Sep 2015 03:16

Hi guys!
I was wondering if someone could please help me with this.
I have a txt file called "cat.txt" in the text file i have this:

this
animal
is
a
cat
but
it's
not
my
cat
but
the
neighbors
cat


I looking for the code to find the word "cat" and to grab the line above, the "cat" line and the line below, so three lines.
And copy everything to a textfile, without the three lines.
Like this:

this
animal
is
it's
not
the

A got this code from somewhere and it grab's the three lines but outputs that to the textfile.


Code: Select all

@echo off
setlocal EnableDelayedExpansion
set lastLine=0
< %~1 > %~n1_nocat.txt (for /F "delims=:" %%a in (
              'findstr /N /I /C:"cat" %1') do (
   set /A skip=%%a-lastLine-1, lastLine=%%a+2
   for /L %%i in (1,1,!skip!) do set /P line=
   for /L %%i in (1,1,3) do set /P "line=!line!" & echo/
   echo/
))

Could someone please help?

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

Re: Don't copy 3 lines to text file?

#2 Post by foxidrive » 26 Sep 2015 03:40

Gamer95 wrote:I looking for the code to find the word "cat" and to grab the line above, the "cat" line and the line below, so three lines.
And copy everything to a textfile, without the three lines.
Like this:

this
animal
is
it's
not
the


What you've shown isn't what you've asked, is it?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Don't copy 3 lines to text file?

#3 Post by ShadowThief » 26 Sep 2015 03:43

foxidrive wrote:
Gamer95 wrote:I looking for the code to find the word "cat" and to grab the line above, the "cat" line and the line below, so three lines.
And copy everything to a textfile, without the three lines.
Like this:

this
animal
is
it's
not
the


What you've shown isn't what you've asked, is it?

He wants to delete the line above each cat, the cat line, and line under each cat.

this
animal
is
a <delete
cat <delete
but <delete
it's
not
my <delete
cat <delete
but <delete
the
neighbors <delete
cat<delete

this
animal
is
it's
not
the

Gamer95
Posts: 32
Joined: 26 Sep 2015 03:05

Re: Don't copy 3 lines to text file?

#4 Post by Gamer95 » 26 Sep 2015 03:44

Yeah i'm looking for that output in a text file.

this
animal
is
it's
not
the

currently with my script i got this:

a
cat
but

my
cat
but

neighbors
cat
cat

So it's grabbing the correct lines but i need the reverse, sorry for my explanation :oops:
Thanks!

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

Re: Don't copy 3 lines to text file?

#5 Post by foxidrive » 26 Sep 2015 03:50

ShadowThief wrote:He wants to delete the line above each cat, the cat line, and line under each cat.


Thanks ShadowThief. You're right and the critical three words "without the three lines" didn't jump out at me.

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

Re: Don't copy 3 lines to text file?

#6 Post by foxidrive » 26 Sep 2015 03:54

Code: Select all

type cat.txt |findrepl "cat" /o:-1:+1 /v


this
animal
is
it's
not
the


This uses a native Windows batch script called findrepl.bat (by Aacini)
findrepl.bat can be downloaded from: https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat
and it can also be found here: viewtopic.php?f=3&t=4697

Place it in the same folder as the batch file, or in a folder that is on the system path.

Gamer95
Posts: 32
Joined: 26 Sep 2015 03:05

Re: Don't copy 3 lines to text file?

#7 Post by Gamer95 » 26 Sep 2015 04:01

That works great foxidrive thank you,
But can it also work without findrepl.bat?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Don't copy 3 lines to text file?

#8 Post by ShadowThief » 26 Sep 2015 04:31

It's rather long, but it works, provided that none of the lines in the source file have spaces.

Code: Select all

@echo off
setlocal enabledelayedexpansion

if exist %~n1_nocat.txt del %~n1_nocat.txt

for /f "tokens=1,2 delims=:" %%A in ('findstr /N /V /C:" " "%~1"') do (
   set line[%%A]=%%B
   set max_num=%%A
)

for /L %%A in (1,1,!max_num!) do (
   set /a prev_line=%%A-1
   set /a next_line=%%A+1
   if "!line[%%A]!"=="cat" (
      if defined line[!prev_line!] set "line[!prev_line!]="
      set "line[%%A]="
      if defined line[!next_line!] set "line[!next_line!]="
   )
)

for /L %%A in (1,1,!max_num!) do (
   if defined line[%%A] echo !line[%%A]!>>%~n1_nocat.txt
)

Gamer95
Posts: 32
Joined: 26 Sep 2015 03:05

Re: Don't copy 3 lines to text file?

#9 Post by Gamer95 » 26 Sep 2015 08:22

Thank you both!

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Don't copy 3 lines to text file?

#10 Post by Aacini » 26 Sep 2015 08:38

Your code almost did it, just needed a couple small changes:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set lastLine=0
< %~1 (for /F "delims=:" %%a in ('findstr /N /I /C:"cat" %1') do (
   set /A get=%%a-lastLine-2, lastLine=%%a+1
   for /L %%i in (1,1,!get!) do set /P "line=" & echo !line!
   for /L %%i in (1,1,3) do set /P "line="
)) > %~n1_nocat.txt

Antonio

Gamer95
Posts: 32
Joined: 26 Sep 2015 03:05

Re: Don't copy 3 lines to text file?

#11 Post by Gamer95 » 26 Sep 2015 09:25

Great Antonio! just great! :D

Post Reply