Page 1 of 1

Get only first row for each file

Posted: 02 Aug 2010 05:37
by darioit
Hello,

I need to take only the first row of each file and write to another file

any suggestion?

Thanks
Dario

Re: Get only first row for each file

Posted: 02 Aug 2010 06:55
by miskox

Code: Select all

@echo off
set /a tmpmatch=0
for /f "delims=" %%i in (a.a) do call :0 "%%i"
echo.%outstr%
goto :EOF

:0
if "%tmpmatch%"=="0" set "outstr=%~1" && set /a tmpmatch=1
goto :EOF


File a.a is your input .txt file. Line echo.%outstr% displays a string. You can redirect it to a file if you wish.

I am sure there are other ways.

Saso

Re: Get only first row for each file

Posted: 02 Aug 2010 07:36
by miskox
Even shorter:

Code: Select all

@echo off
set "outstr="
for /f "delims=" %%i in (a.a) do if not defined outstr set outstr=%%i
echo.%outstr%


Saso

Re: Get only first row for each file

Posted: 02 Aug 2010 08:26
by aGerman
Give that a try

Code: Select all

@echo off &setlocal
set "extension=*.txt"
set "outfile=firstLines.txt"

>"%outFile%" type nul
for /f "delims=" %%a in ('dir /b %extension%^|findstr /i /x /v /c:"%outfile%"') do (
  set /p "line="<"%%a"
  >>"%outFile%" call echo.%%line%%
)
pause


Note: This will not work if there are special characters like &<>| in the lines. In this case come back with that information.

Regards
aGerman

Re: Get only first row for each file

Posted: 02 Aug 2010 10:49
by avery_larry
Unlike the first 2 ideas, this code *should* process faster as it will only process the first line of the file and then continue on to the next file (instead of looping through the entire file just to get the first line).

I can't comment on aGerman's code, since I frankly haven't figured out how it works (it's over my head!).

Code: Select all

@echo off
for %%a in (*) do call :process "%%~a"
echo The rest of your commands go here.
goto :eof


:process
for /f "usebackq delims=" %%a in (%1) do echo.%%a>>outfile.txt&goto :eof

Re: Get only first row for each file

Posted: 02 Aug 2010 16:42
by darioit
Thanks to all for the reply
the 1st script is too slow for big file, the second script from Saso is faster enought
the 4st script from avery_larry is like mine (mine script doesn't work fine because I miss "&goto :eof" at the end of loop)

But the 3st from aGerman is really stunning and works great, I like to study it better for my next script

Thanks again to all
Best
Dario

Re: Get only first row for each file

Posted: 02 Aug 2010 20:24
by ghostmachine4
download sed for windows

Code: Select all

@echo off
for /f %%a in ('dir /B *.txt') do (
  sed -n "1p;q" "%%a"
)

Re: Get only first row for each file

Posted: 04 Aug 2010 03:24
by darioit
Finally I chose this one that works fine in this way
for /f "usebackq delims=" %%a in (%1) do echo.%%a>> C:\output.txt&goto :eof

But I prefer take only 80 columns in the first raw like this

for /f "usebackq delims=" %%a in (%1) do echo.%%a:~0,80%%%>> C:\output.txt&goto :eof
for /f "usebackq delims=" %%a in (%1) do echo.%%%a:~0,80%%%>> C:\output.txt&goto :eof
for /f "usebackq delims=" %%a in (%1) do echo %%a:~0,80%%%>> C:\output.txt&goto :eof
for /f "usebackq delims=" %%a in (%1) do echo %%a:~0,80%% C:\output.txt&goto :eof
for /f "usebackq delims=" %%a in (%1) do echo %%%a:~0,80%%% >> C:\output.txt&goto :eof

I try many way but none is working, why?

Re: Get only first row for each file

Posted: 04 Aug 2010 07:02
by !k
darioit, impossible to use an expression like "%%a:~0,80"
You must first assign a "%%a" to the new variable, and then work with her.

Code: Select all

:process
for /f "usebackq delims=" %%a in (%1) do (
set "var=%%~a"
>>outfile.txt echo !var:~0,80!
goto :eof)

Re: Get only first row for each file

Posted: 04 Aug 2010 13:33
by aGerman
Only additional to !k's post:
You have to define SetLocal EnableDelayedExpansion if you want to use exclamation marks like percent signs.

Otherwise use the Call Trick:

Code: Select all

:process
for /f "usebackq delims=" %%a in (%1) do (
  set "var=%%~a"
  >>outfile.txt call echo %%var:~0,80%%
  goto :eof
)


Regards
aGerman

Re: Get only first row for each file

Posted: 22 May 2014 17:02
by Sponge Belly
Hello All! :-)

Sorry to revive an old topic, but avery_larry seems to be saying that the following:

Code: Select all

for /f "usebackq delims=" %%f in ("file.txt") do (
echo(%%f
goto break
)

:break
(rest of program)


will display the first non-empty line of file.txt without the overhead of expanding the entire file inside the for /f loop’s in (…) clause. Did I understand him correctly?

If so, then this method is superior to:

Code: Select all

set line=
set /p line= < file.txt


because of set /p’s limitations, imho… but I thought I'd better check with the experts before I made any rash statements. ;-)

- SB