Reputation: 111
This is my text file and I want to remove first line (with blank):
abc!
def #
ghi./;
jklm
nopqrs
How can I remove first line from my text file?
I want my second line be first line:
def #
ghi./;
jklm
nopqrs
I tried findstr
command, but it doesn't work.
Upvotes: 7
Views: 29801
Reputation: 1
: Execute the command WMIC and remove the first line and the last blank line. This can : be easily modified to work on a file too. I just needed a viable replacement for : the older "NET VIEW" command.
for /f "skip=1 delims=$" %f in ('"wmic /NAMESPACE:\root\directory\ldap PATH ds_computer GET ds_samaccountname "') do @for /f %g in ("%f") do @echo \%g
Upvotes: 0
Reputation: 1261
I suggest tail to output lines starting with line number 2:
tail -n +2 in.txt > out.txt
From the man page:
-n, --lines=[+]NUM
output the last NUM lines, instead of the last 10; or use -n
+NUM to output starting with line NUM
Upvotes: 2
Reputation: 519
If you like using Vim with CMD you can use: gvim -c "execute \"normal! #dd\"" file.txt
, where "#" is the number of lines you want to delete off of the top of the text file ("#" could "1", "82", or another number). For this question you want gvim -c "execute \"normal! 1dd\"" file.txt
.
Upvotes: 0
Reputation: 34909
The easiest approach (assuming your text file is called data.txt
):
more +1 "data.txt" > "data_NEW.txt"
This limits lines to a length of about 65535 bytes/characters and the file to about 65535 lines. Furthermore, TABs become expanded to SPACEs (8 by default).
You could use a for /F
loop instead (the odd-looking unquoted option string syntax is needed here in order to disable the default eol
character ;
to not ignore lines beginning with such):
@echo off
(
for /F usebackq^ skip^=1^ delims^=^ eol^= %%L in ("data.txt") do echo(%%L
) > "data_NEW.txt"
This limits lines to a length of about 8190 characters/bytes and loses empty lines.
You could use a for /F
loop together with findstr
to keep empty lines (findstr
adds a line number plus :
to each line, so for /F
does not see empty lines; everything up to the (first) colon is then removed in the loop body; toggling delayed expansion ensures not to lose !
):
@echo off
(
for /F "skip=1 delims=" %%L in ('findstr /N "^" "data.txt"') do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
echo(!LINE:*:=!
endlocal
)
) > "data_NEW.txt"
This still limits lines to a length of about 8190 characters/bytes.
Or you could use input redirection <
together with set /P
(for this the total number of lines needs to be determined in advance):
@echo off
for /F %%C in ('find /C /V "" ^< "data.txt"') do set "COUNT=%%C"
setlocal EnableDelayedExpansion
(
for /L %%I in (1,1,%COUNT%) do (
set "LINE=" & set /P LINE=""
if %%I gtr 1 echo(!LINE!
)
) < "data.txt" > "data_NEW.txt"
endlocal
This limits lines to a length of about 1022 characters/bytes.
To replace your original file with the modified one, simply do this:
move /Y "data_NEW.txt" "data.txt" > nul
Upvotes: 14
Reputation: 16236
This is pretty easy with PowerShell. If the file is not large, Get-Content
works well.
Get-Content -Path .\t.txt | Select-Object -Skip 1
If you must run from a cmd.exe shell, then use:
powershell -NoProfile -Command "Get-Content -Path .\t.txt | Select-Object -Skip 1"
Upvotes: 0
Reputation: 80023
From the prompt,
for /f "skip=1delims=" %a in (yourtextfilename) do >>anoutputfilename echo %a
where yourtextfilename
& anoutputfilename
must be different filenames and need to be "quoted" if they contain spaces or other separators.
Upvotes: 1