Extract first and last line in batch
Solved
Mael730
Posted messages
50
Status
Member
-
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!!
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
If you are on Linux, you can use the head and tail commands:
--
ubuntu 9.04
touch toto # first line head --lines=1 fichier.txt >> toto # last line tail --lines=1 fichier.txt >> toto
--
ubuntu 9.04
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!!??
@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" )4.134766E+00 -2.000232E+00 1.700000E+01
@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" )```