Page 1 of 1

Removing second last line from multiple text files

Posted: 14 Nov 2014 15:19
by KD22
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.

Re: Removing second last line from multiple text files

Posted: 14 Nov 2014 15:58
by Squashman
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
)

Re: Removing second last line from multiple text files

Posted: 14 Nov 2014 17:06
by foxidrive
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.

Re: Removing second last line from multiple text files

Posted: 15 Nov 2014 00:20
by dbenham
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

Re: Removing second last line from multiple text files

Posted: 15 Nov 2014 01:32
by goldfish
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

Re: Removing second last line from multiple text files

Posted: 15 Nov 2014 06:25
by Aacini
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! :?

Re: Removing second last line from multiple text files

Posted: 15 Nov 2014 08:23
by dbenham
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

Re: Removing second last line from multiple text files

Posted: 15 Nov 2014 15:07
by Compo
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.

Re: Removing second last line from multiple text files

Posted: 15 Nov 2014 22:12
by foxidrive
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.

Re: Removing second last line from multiple text files

Posted: 16 Nov 2014 08:57
by Compo
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?

Re: Removing second last line from multiple text files

Posted: 16 Nov 2014 09:33
by foxidrive
EDIT: Irrelevant post snipped

Re: Removing second last line from multiple text files

Posted: 16 Nov 2014 14:49
by Squashman
65,000 lines is a small file in my world of data processing.

Re: Removing second last line from multiple text files

Posted: 17 Nov 2014 03:28
by Compo
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?

Re: Removing second last line from multiple text files

Posted: 17 Nov 2014 04:59
by foxidrive
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

Re: Removing second last line from multiple text files

Posted: 17 Nov 2014 05:53
by Compo
Thanks for letting me know!