Extract first and last line in batch

Solved
Mael730 Posted messages 50 Status Member -  
Mael730 Posted messages 50 Status Member -
Bonjour,

Here is my problem, I have a .txt file from which I want to extract the first and the last line to copy them into another .txt file:

4.136377E+00 -2.000195E+00 6.000000E+00
4.136230E+00 -2.000146E+00 7.000000E+00
4.135986E+00 -2.000232E+00 8.000000E+00
4.135889E+00 -2.000177E+00 9.000000E+00
4.135742E+00 -2.000171E+00 1.000000E+01
.
.
.
4.135156E+00 -2.000140E+00 1.400000E+01
4.135010E+00 -2.000183E+00 1.500000E+01
4.134766E+00 -2.000134E+00 1.600000E+00
4.134766E+00 -2.000232E+00 1.700000E+01

So I would like:

4.136377E+00 -2.000195E+00 6.000000E+00
4.134766E+00 -2.000232E+00 1.700000E+01

I thought about using findstr, but since in the first and last line there are no distinguishing data from the other lines, I don't know how to proceed??

P.S: batch required!!
Configuration:  Windows XP Firefox 3.5.3

5 answers

  1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    bonjour
    @echo off set c=1 setlocal enableDelayedExpansion for /f "delims=" %%a in ('type fichier.txt') do ( if !c! EQU 1 echo %%a set /a c+=1 set d=%%a ) echo %d% 
    2
    1. Mael730 Posted messages 50 Status Member 1
       
      It only displays the last line of the txt file for me??
      0
    2. Mael730 Posted messages 50 Status Member 1 > dubcek Posted messages 18627 Registration date   Status Contributor Last intervention  
       
      My bad, it actually works!
      I would like to extend the script's work to my entire directory structure, can you tell me what's wrong with what I wrote:

      @echo off

      set c=1
      setlocal enableDelayedExpansion
      for /f "delims=" %%a in ('dir /b/s *.txt') do (
      set E=%%~pa
      if !c! EQU 1 echo %%a >> "!E!resultat.txt"
      set /a c+=1
      set d=%%a
      )
      echo %d% >> "!E!resultat.txt"

      exit

      The script would then go through all the directories and subdirectories looking for .txt files to extract the first and last line and copy them into a result file.
      In the above script, the result file contains the paths of the .txt files in the directory!!??
      0
    3. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660 > Mael730 Posted messages 50 Status Member
       
      It takes 2 loops, one that finds the file names, the other, nested within the first, that reads the files:
      @echo off setlocal enableDelayedExpansion for /f "delims=" %%f in ('dir /b/s *.txt') do ( set E=%%~pf set c=1 for /f "delims=" %%a in ('type "%%f"') do ( if !c! EQU 1 echo %%a >> "!E!resultat.txt" set /a c+=1 set d=%%a ) echo !d! >> "!E!resultat.txt" ) 
      0
    4. Mael730 Posted messages 50 Status Member 1 > dubcek Posted messages 18627 Registration date   Status Contributor Last intervention  
       
      4.136377E+00 -2.000195E+00 6.000000E+00
      4.134766E+00 -2.000232E+00 1.700000E+01
      0
    5. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660 > Mael730 Posted messages 50 Status Member
       
      ```plaintext You need to rewrite the loop to display only the first line of the findstr output
      @echo off setlocal enableDelayedExpansion for /f "delims=" %%f in ('dir /b/s *.txt') do ( set E=%%~pf set c=1 for /f "delims=" %%x in ('findstr "^[0-9]" "%%f"') do ( if !c! EQU 1 echo %%x >> "!E!resultat.txt" set /a c+=1 ) for /f "delims=" %%a in ('type "%%f"') do set d=%%a echo !d! >> "!E!resultat.txt" )
      ```
      0
  2. ladgalen Posted messages 158 Status Member 10
     
    If you are on Linux, you can use the head and tail commands:

     touch toto # first line head --lines=1 fichier.txt >> toto # last line tail --lines=1 fichier.txt >> toto 


    --
    ubuntu 9.04
    1
  3. ladgalen Posted messages 158 Status Member 10
     
    Oops, I mixed up batch (DOS) and bash (Linux)... maybe tail and head exist in DOS!
    --
    ubuntu 9.04
    0
    1. greg
       
      How can one come up with such nonsense?!
      We shouldn't be asking "maybe"....
      We ask Google and we have the answer if we can't read the online help
      Serious.
      0
  4. ladgalen Posted messages 158 Status Member 10
     
    mmmhhh

    who said that Linux is complicated ;)

    PS: sorry for the post pollution

    --
    ubuntu 9.04
    0
  5. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    too easy on Linux, there are all the tools :-) ...you can also do it in one command with awk
    awk 'NR==1{print $0};END{print $0}' < file.txt
    0