Although you did not show us your own efforts on the problem at hand, I decided to provide some code, because the task seems not that particularly trivial to me...
The scripts below remove the first word from the first line.
The following one reads the given text file using a for /F
loop and splits off the first word in the first line by another nested for /F
loop; the remaining lines are returned unedited:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FLAG=" & rem // (this marks the first line)
for /F "delims=" %%L in ('findstr /N /R "^" "%~1"') do (
set "LINE=%%L"
rem // (toggle delayed expansion to not lose `!`)
setlocal EnableDelayedExpansion
set "LINE=!LINE:*:=!"
if defined FLAG (
rem // (this is executed for all but the first lines)
echo(!LINE!
) else (
rem // (this is executed for the first line only)
if defined LINE (
rem // (an empty line skipped over the loop)
for /F "tokens=1,*" %%E in ("!LINE!") do (
endlocal
rem // (return line with first word removed)
echo(%%F
setlocal EnableDelayedExpansion
)
) else echo/
)
endlocal
rem // (set this after the first loop iteration)
set "FLAG=#"
)
endlocal
exit /B
This one reads the given text file via redirection, splits off the first word of the first line again by a for /F
loop and returns the remaining lines by a findstr
command line:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
< "%~1" (
rem // (capture first line here)
set /P "LINE="
rem /* (the `echo` in the pipe below runs in Command Prompt context,
rem so an empty variable `LINE` literally returned `!LINE!) */
if defined LINE (
rem /* (this is executed for the first line only;
rem delay variable expansion as much as possible) */
for /F "tokens=1,*" %%I in ('cmd /V /C echo(!LINE!^| findstr /N /R "^"') do (
rem // (return line with first word removed)
echo(%%J
)
) else echo/
rem // (this is executed for all but the first lines)
findstr /R "^"
)
endlocal
exit /B
Both scripts expect the input text file to be provided as a command line argument. Supposing either script is stored as remove-first-word.bat
and the text file is called sample.txt
, the command line to be used is as follows:
remove-first-word.bat "sample.txt"
or:
remove-first-word.bat "\path\to\sample.txt"
To write the output into another file, say return.txt
, rather than to the console, use redirection:
remove-first-word.bat "sample.txt" > "return.txt"
or:
remove-first-word.bat "\path\to\sample.txt" > "\path\to\return.txt"