2

I need to copy the bottom 16 lines from a text file to another text file. I need to do this procedure for all clients. At the client's location the text file will be common but the bottom 16 lines is important for confirmation of package installation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user73628
  • 3,715
  • 5
  • 29
  • 24
  • Possible dupe: http://stackoverflow.com/questions/523181/cmd-exe-batch-script-to-display-last-10-lines-from-a-txt-file – Joey Oct 27 '09 at 22:24

3 Answers3

6

The more command can be used to extract the last n lines:

  1. If a file, someFile.txt, contains 2000 lines then the last 16 lines can be extracted with ("/E +n: Start displaying the first file at line n"):

    more /e +1984 someFile.txt > lastLines.txt
    
  2. The number of lines in someFile.txt can found as:

    for /f %%i in ('find /v /c "" ^< someFile.txt') do set /a lines=%%i
    
  3. The call of more then becomes (still for this example, the last 16 lines):

    set /a startLine=%lines% - 16
    more /e +%startLine% someFile.txt > lastLines.txt
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

You can download DOS ports of most Unix commands (for example here - pick any set of commands you like that includes tail)

After downloading, simply use tail -16 filename.txt

The benefit (to offset the effort of downloading/unpacking) is that you get a whole BUNDLE of really good Unix command line tools to use.

DVK
  • 126,886
  • 32
  • 213
  • 327
0

I adapted this useful code to append together 51 files and retain the 12 line header of the first file as follows:

REM Append 51 files and retain 12 line header of first file
REM  ------------------------------------------------------

REM Set number of files to combine    
set Nmbrfls=51   

REM copy the first file with the header 
copy file_1.txt combined.txt

REM Loop through the other 50 files (start at #2) appending to the combined
REM file using a temporary file to capture all but the the 12 header lines
REM than append the temporary file to the combined on each loop

for /l %%i in (2,1,%Nmbrfls%) do (
more /e +13 file_%%i.txt > temp.txt
copy /b combined.txt + temp.txt combined.txt
del temp.txt
)
Markm0705
  • 1,340
  • 1
  • 13
  • 31