Removing second last line from multiple text files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
KD22
Posts: 1
Joined: 14 Nov 2014 15:12

Removing second last line from multiple text files

#1 Post by KD22 » 14 Nov 2014 15:19

I have multiple .TXT files where the second last line is a summary of observations (ie. (452,246) ).

While searching through forums, I have come upon this code:

Code: Select all

@echo off
setlocal disableDelayedExpansion
set file="test.txt"
for /f %%N in ('find /c /v "" ^<%file%') do set skip=%%N
(
for /f "delims=" %%L in ('findstr /n "^" %file%^|findstr /v "^%skip%:"') do (
set "ln=%%L"
setlocal enableDelayedExpansion
echo(!ln:*:=!
endlocal
)
)>%file%.new
move %file%.new %file% >nul


This code works perfectly on a single file, but I'm not sure how to alter this to run it against all files in my folder (*.txt).

Thanks in advance for your help.

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

Re: Removing second last line from multiple text files

#2 Post by Squashman » 14 Nov 2014 15:58

Essentially wrap another FOR command around it.

Untested

Code: Select all

@echo off
setlocal disableDelayedExpansion
FOR %%G in (*.txt) DO (
for /f %%N in ('find /c /v "" ^<"%%~G"') do set skip=%%N
(
for /f "delims=" %%L in ('findstr /n "^" "%%~G"^|findstr /v "^%skip%:"') do (
   set "ln=%%L"
   setlocal enableDelayedExpansion
   echo(!ln:*:=!
   endlocal
   )
)>"%%~G.new"
move "%%~G.new" "%%~G" >nul
)
Last edited by Squashman on 14 Nov 2014 16:02, edited 1 time in total.
Reason: Forgot to change the code for the output file names.

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

Re: Removing second last line from multiple text files

#3 Post by foxidrive » 14 Nov 2014 17:06

KD22 wrote:I have multiple .TXT files where the second last line is a summary of observations (ie. (452,246) ).

It also helps to describe your task using english, so there are fewer misunderstandings.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Removing second last line from multiple text files

#4 Post by dbenham » 15 Nov 2014 00:20

Your title says you want to remove the 2nd last line, but your code removes the last line. I'm assuming your code is correct.

This is a simple and efficient solution (to remove the last line using the new JREPL.BAT

Code: Select all

@echo off
for /f "eol=: delims=" %%F in ('dir /b *.txt') do (
  call jrepl "^.*\n(?=[\s\S])" "output.write($0);''" /jmatch /m /f "%%F" /o -
)


Dave Benham
Last edited by dbenham on 15 Nov 2014 07:52, edited 1 time in total.

goldfish
Posts: 10
Joined: 31 Mar 2014 07:02

Re: Removing second last line from multiple text files

#5 Post by goldfish » 15 Nov 2014 01:32

dbenham wrote:Your title says you want to remove the 2nd last line, but your code removes the last line. I'm assuming your code is correct.

This is a simple and efficient solution using the new JREPL.BAT

Code: Select all

@echo off
for /f "eol=: delims=" %%F in ('dir /b *.txt') do (
  call jrepl "^.*\n(?=[\s\S])" "output.write($0);''" /jmatch /m /f "%%F" /o -
)


Dave Benham

that doesn't remove the 2nd last line

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

Re: Removing second last line from multiple text files

#6 Post by Aacini » 15 Nov 2014 06:25

Perhaps is this what you want?

Code: Select all

C:\> type theFile.txt
First line
Second line
Third Line
2nd last line
Last line


C:\> < theFile.txt FindRepl /O:-2:-2
2nd last line

C:\> < theFile.txt FindRepl /O:-2:-2 /V
First line
Second line
Third Line
Last line

If so, then you may achieve the same thing in all *.txt files this way:

Code: Select all

@echo off

for %%a in (*.txt) do < "%%a" call FindRepl /O:-2:-2 /V > "%%~Na.new"
del *.txt
ren *.new *.txt

You may download FindRepl.bat program from this site

Antonio

PS - **EDIT**: Stupid mistake fixed! :?
Last edited by Aacini on 15 Nov 2014 20:36, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Removing second last line from multiple text files

#7 Post by dbenham » 15 Nov 2014 08:23

If you truly want to remove the 2nd to last line, and preserve the last line, you can still use JREPL.BAT:

Code: Select all

@echo off
for /f "eol=: delims=" %%F in ('dir /b *.txt') do (
  call jrepl "^.*\n(?=.*\n[\s\S])|^.*\n?(?![\s\S])" "output.write($0);''" /jmatch /m /f "%%F" /o -
)


Dave Benham

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Removing second last line from multiple text files

#8 Post by Compo » 15 Nov 2014 15:07

If you believe that the summary line is unique within each file then something like this may suit

Code: Select all

:Sub
PushD %~dp1
Set _="%~dp0tmp.txt"
Type Nul>%_%
For /F "Delims=:" %%A In ('FindStr/N "^" "%~nx1"') Do Set/A_lc=%%A-2
More +%_lc%<"%~nx1">%_%
Set/P_rl=<%_%
FindStr/xvc:"%_rl%" "%~nx1">%_%&&Del "%~nx1"&&Ren %_% "%~nx1"
For several txt files you'd obviously Call :Sub with each text file as the parameter.

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

Re: Removing second last line from multiple text files

#9 Post by foxidrive » 15 Nov 2014 22:12

Compo wrote:

Code: Select all

More +%_lc%<"%~nx1">%_%



EDIT: My comment below doesn't apply in this case. Mea Culpa.

Just clarifying that more pauses in a script at each 65K lines, so the makeup of the text files is important.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Removing second last line from multiple text files

#10 Post by Compo » 16 Nov 2014 08:57

foxidrive wrote:Just clarifying that more pauses in a script at each 65K lines, so the makeup of the text files is important.
Curiosity questions…
I often hear them mentioned but have never come across a txt file with this many lines.
  1. What size would we be looking at for a usual txt file to reach the 65000 lines
  2. If I've already pre determined that there are 72458 lines and ask more to start at line 72456, why does it pause before its start point?

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

Re: Removing second last line from multiple text files

#11 Post by foxidrive » 16 Nov 2014 09:33

EDIT: Irrelevant post snipped

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

Re: Removing second last line from multiple text files

#12 Post by Squashman » 16 Nov 2014 14:49

65,000 lines is a small file in my world of data processing.

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Removing second last line from multiple text files

#13 Post by Compo » 17 Nov 2014 03:28

Squashman wrote:65,000 lines is a small file in my world of data processing.

Squashman, can you check if there is a pause using more +n as in my example and feed back then please?

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

Re: Removing second last line from multiple text files

#14 Post by foxidrive » 17 Nov 2014 04:59

Compo wrote:If I've already pre determined that there are 72458 lines and ask more to start at line 72456, why does it pause before its start point?]


I misunderstood this to mean that you'd tried it and it failed.

Your use of more in this way works fine.

Sorry for my comments without really studying the code or testing it. I blame it on old age. ;)

Code: Select all

@echo off

(for /L %%a in (1,1,72458) do (
echo %%a
)) >file.txt

more +72456 file.txt

pause

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Removing second last line from multiple text files

#15 Post by Compo » 17 Nov 2014 05:53

Thanks for letting me know!

Post Reply