Get only first row for each file
Moderator: DosItHelp
Get only first row for each file
Hello,
I need to take only the first row of each file and write to another file
any suggestion?
Thanks
Dario
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
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
Even shorter:
Saso
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
Give that a try
Note: This will not work if there are special characters like &<>| in the lines. In this case come back with that information.
Regards
aGerman
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
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
Re: Get only first row for each file
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!).
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
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
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
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Get only first row for each file
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
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?
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
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.
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
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:
Regards
aGerman
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
-
- Posts: 223
- Joined: 01 Oct 2012 13:32
- Location: Ireland
- Contact:
Re: Get only first row for each file
Hello All!
Sorry to revive an old topic, but avery_larry seems to be saying that the following:
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:
because of set /p’s limitations, imho… but I thought I'd better check with the experts before I made any rash statements.
- SB
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