Get only first row for each file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 212
Joined: 02 Aug 2010 05:25

Get only first row for each file

#1 Post by darioit » 02 Aug 2010 05:37

Hello,

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

any suggestion?

Thanks
Dario

miskox
Posts: 356
Joined: 28 Jun 2010 03:46

Re: Get only first row for each file

#2 Post by miskox » 02 Aug 2010 06:55

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

miskox
Posts: 356
Joined: 28 Jun 2010 03:46

Re: Get only first row for each file

#3 Post by miskox » 02 Aug 2010 07:36

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

aGerman
Expert
Posts: 3937
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get only first row for each file

#4 Post by aGerman » 02 Aug 2010 08:26

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Get only first row for each file

#5 Post by avery_larry » 02 Aug 2010 10:49

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

darioit
Posts: 212
Joined: 02 Aug 2010 05:25

Re: Get only first row for each file

#6 Post by darioit » 02 Aug 2010 16:42

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Get only first row for each file

#7 Post by ghostmachine4 » 02 Aug 2010 20:24

download sed for windows

Code: Select all

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

darioit
Posts: 212
Joined: 02 Aug 2010 05:25

Re: Get only first row for each file

#8 Post by darioit » 04 Aug 2010 03:24

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?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Get only first row for each file

#9 Post by !k » 04 Aug 2010 07:02

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)

aGerman
Expert
Posts: 3937
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Get only first row for each file

#10 Post by aGerman » 04 Aug 2010 13:33

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

Sponge Belly
Posts: 200
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Get only first row for each file

#11 Post by Sponge Belly » 22 May 2014 17:02

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

Post Reply