Skip lines in a text file from top and bottom with query

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Chronofos
Posts: 6
Joined: 16 Mar 2024 11:09

Skip lines in a text file from top and bottom with query

#1 Post by Chronofos » 25 Mar 2024 03:35

I'm currently trying to learn how to work with batch files using various tutorials. During the "battle" with the FOR loop, I came across several times that the skip parameter only cuts off the lines from the top.
I tried to write a batch program that also trims the bottom lines. A kind of skip area. This seems to be working now. Also thanks to the help from this forum.
Not that the program not really useful in this form, but perhaps someone can use one or two subroutines.

Program call with name of the text file
/help - This help text
/use - Short explanation of the program

Code: Select all

@ECHO OFF
CLS
CHCP 1252 > nul

setlocal enabledelayedexpansion

:MAIN

REM  ****************************************************************************************************
REM  * foreplay                                                                                         *
REM  ****************************************************************************************************
CALL :help_and_error_handling %1
CALL :remove_empty_lines %1

REM  ****************************************************************************************************
REM  * Part 1: Numbering - Cropping                                                                     *
REM  ****************************************************************************************************
CALL :line_numbering %1
CALL :cleanup %1
CALL :type %1
CALL :read_number_of_lines_to_skip %1
CALL :skip_lines %1
CALL :cleanup %1

REM  ****************************************************************************************************
REM  * Part 2: Numbering - Sorting descending - Removing numbering - Cropping                           *
REM  ****************************************************************************************************
CALL :line_numbering %1
CALL :cleanup %1
CALL :sort_descending %1
CALL :cleanup %1
CALL :remove_numbering %1
CALL :cleanup %1
CALL :line_numbering %1
CALL :cleanup %1
CALL :type %1
CALL :read_number_of_lines_to_skip %1
CALL :skip_lines %1
CALL :cleanup %1

REM  ****************************************************************************************************
REM  * Part 3: Numbering - Sorting descending - Remove numbering                                        *
REM  ****************************************************************************************************
CALL :line_numbering %1
CALL :cleanup %1
CALL :sort_descending %1
CALL :cleanup %1
CALL :remove_numbering %1
CALL :cleanup %1

ECHO   ****************************************************************************************************
ECHO   * The result is displayed                                                                          *
ECHO   * and stored as filename_new.txt in the same directory.                                            *
ECHO   ****************************************************************************************************
ECHO.
CALL :type %1

ren %~d1%~p1%~n1_temp_01%~x1 %~n1_new%~x1

GOTO :EOF

REM  ****************************************************************************************************
REM  * SUBROUTINEN_IN_ALPHABETICAL_ORDER                                                                *
REM  *==================================================================================================*
REM  * 1 = cleanup                                                                                      *
REM  * 2 = help_and_error_handling                                                                      *
REM  * 3 = line_numbering                                                                               *
REM  * 4 = read_number_of_lines_to_skip                                                                 *
REM  * 5 = remove_empty_lines                                                                           *
REM  * 6 = remove_numbering                                                                             *
REM  * 7 = skip_lines                                                                                   *
REM  * 8 = sort_descending                                                                              *
REM  * 9 = type                                                                                         *
REM  ****************************************************************************************************

:cleanup
REM   *******************************************************************************************
REM   * Cleanup                                                                                 *
REM   *******************************************************************************************
del %~d1%~p1%~n1_temp_01%~x1                          2> nul
ren %~d1%~p1%~n1_temp_02%~x1 %~n1_temp_01%~x1         2> nul
GOTO :EOF

:help_and_error_handling
REM   *******************************************************************************************
REM   * Catching errors and output help text                                                    *
REM   *******************************************************************************************
if exist %~d1%~p1%~n1_new%~x1 (
     ECHO   **********************************
     ECHO   *   Output file exists^^! Abort^^!   *
     ECHO   **********************************
     ECHO.
     cmd /k
)    else if /I "%~1"=="" (
     ECHO   ***********************************
     ECHO   *   No parameter passed^^! Abort^^!   *
     ECHO   ***********************************
     ECHO.
     cmd /k
)    else if /I "%~1"=="/help" (
     ECHO   **************************************************
     ECHO   *   Program call with name of the text file      *
     ECHO   *   /help   - This help text                     *
     ECHO   *   /use    - Short explanation of the program   *
     ECHO   **************************************************
     ECHO.
     cmd /k
)    else if /I "%~1"=="/use" (
     ECHO   *******************************************************************
     ECHO   * The program cuts a specified number of lines from the beginning *
     ECHO   * and from the end of a text file and saves it with a new name    *
     ECHO   * ^(Filename_new.txt^) in the same directory.                       *
     ECHO   * The original file is retained^^!                                  *
     ECHO   *******************************************************************
     cmd /k
)    else if NOT exist "%~1" (
     ECHO   **********************************
     ECHO   *   File doesn't exist^^! Abort^^!   *
     ECHO   **********************************
     ECHO.
     cmd /k
)    else if /I "%~x1"=="" (
     ECHO   *********************************
     ECHO   *   No file extension^^! Abort^^!   *
     ECHO   *********************************
     ECHO.
     cmd /k
)    else if /I NOT "%~x1"==".txt" (
     ECHO   *****************************************
     ECHO   *   File extension is not txt^^! Abort^^!   *
     ECHO   *****************************************
     ECHO.
     cmd /k
)    else if /I "%~x1"==".txt" (
     REM
)
GOTO :EOF

:line_numbering
REM   *******************************************************************************
REM   * After the upper "unuseful" lines have been removed from the text file,      *
REM   * The lines in this "skipped" text file are now numbered.                     *
REM   * A separate routine is used for this, as the numbering is done by findst     *
REM   * "only" counts: 1,2,3,4,5,6,7,8,9,10,11...100,101 !                          *
REM   * But you can't sort with that!                                               *
REM   * Therefore own numbering 0001,0002....0010, 0011,...0100....1000             *
REM   * This only goes up to 9999 lines, then you would have to add another ELSE IF *
REM   *******************************************************************************

set /A line_counter=0

FOR /F "delims="" tokens=*" %%A IN (%~d1%~p1%~n1_temp_01%~x1) DO (SET /A line_counter=line_counter+1
    IF !line_counter! LSS 10  (ECHO 000!line_counter! %%A >> %~d1%~p1%~n1_temp_02%~x1
    ) else if !line_counter! LSS 100 (ECHO 00!line_counter! %%A >> %~d1%~p1%~n1_temp_02%~x1
    ) else if !line_counter! LSS 1000 (ECHO 0!line_counter! %%A >> %~d1%~p1%~n1_temp_02%~x1
    ) else (ECHO !line_counter! %%A >> %~d1%~p1%~n1_temp_02%~x1
    )
    )
)
GOTO :EOF

:read_number_of_lines_to_skip
REM   ****************************************************************************************
REM   * Query the number of lines to skip.                                                   *
REM   ****************************************************************************************
ECHO.
ECHO   ****************************************************************************************
ECHO   * If not all lines are displayed, scroll up.                                           *   
ECHO   ****************************************************************************************
ECHO.
set /p lines_to_skip=Specify number of lines to skip:  
ECHO.
GOTO :EOF

:remove_empty_lines
REM   **********************************************************************************************
REM   * Since we will continue to work with the FOR loop later on                                  *
REM   * and therefore the empty lines would be omitted anyway, so it is not advisable              *
REM   * to count the number of lines in the original text file!                                    *
REM   * Because after processing through another FOR loop, the number of lines would be incorrect. *
REM   * Therefore, remove the empty lines using a FOR loop.                                        *
REM   **********************************************************************************************
   for /f "delims=" %%i in (%1) do echo %%i >> %~d1%~p1%~n1_temp_01%~x1
GOTO :EOF

:remove_numbering
REM   ****************************************************************************************
REM   * Removing numbering                                                                   *
REM   ****************************************************************************************
for /f "tokens=2,*" %%i in (%~d1%~p1%~n1_temp_01%~x1) do echo %%i %%j >> %~d1%~p1%~n1_temp_02%~x1
GOTO :EOF

:skip_lines
REM   ****************************************************************************************
REM   * Saving the "skipped" text file without line numbering                                *
REM   ****************************************************************************************
for /f "skip=%lines_to_skip% tokens=2,*" %%i in (%~d1%~p1%~n1_temp_01%~x1) do echo %%i %%j >> %~d1%~p1%~n1_temp_02%~x1
GOTO :EOF

:sort_descending
REM   *******************************************************************************************
REM   * Sort descending                                                                         *
REM   *******************************************************************************************
sort /r %~d1%~p1%~n1_temp_01%~x1 >> %~d1%~p1%~n1_temp_02%~x1
GOTO :EOF

:type
REM   *******************************************************************************************
REM   * Display the file                                                                        *
REM   *******************************************************************************************
type %~d1%~p1%~n1_temp_01%~x1
GOTO :EOF

Post Reply