Extract N lines from file using single window command

Is there a way to extract / copy fist X the number of lines from a file and enter them into another file with one command using the windows command line?

I can remove the first number of X lines using:
more + X [file_contain data]> [file_to_export_data_to]

If the head command works, I think I can just do this:
head -X [file_containing data]> [file_to_export_data_to]

But this, unfortunately, does not work.

It would be great if Windows had a less command, but again no luck.

I'm a complete newbie when it comes to this, so I'm sure I have something obvious. I do not want to install anything or use anything other than the command line.

thanks

+9
windows cmd command-prompt
source share
7 answers

You can use PowerShell from cmd.exe console:

powershell -command "& {get-content input.txt|select-object -first 10}" >output.txt 

You can create a DOSKEY macro to make it easier to use from the command line:

 doskey head=powershell -command "& {get-content $1|select-object -first $2}" 

Using:

 head input.txt 10 >output.txt 

But you cannot use the DOSKEY macro in a script package.

Instead, you can create a head.bat script and put it in the folder that is included in your PATH:

head.bat

 @powershell -command "& {get-content %1|select-object -first %2}" 

At the command line you should use head input.txt 10 >output.txt

From inside the script package you should use call head input.txt 10 >output.txt

I decided not to have the output file as a parameter if you just want to display the result on the screen instead of writing to the file.

+16
source share

The simplest one-command solution is to use Powershell Get-Content.

N is the number of rows.

From the beginning of the file:

 Get-Content -Head N file.txt 

At the end of the file:

 Get-Content -Tail N file.txt 
+15
source share
 (@FOR /f "tokens=1* delims=:" %a IN ('findstr /n "^" "standardwaffle.txt"') DO @IF %a leq 7 ECHO(%b)>u:\junk.txt 

will extract the first 7 lines from standardwaffle.txt to u:\junk.txt , so here it is on the same cmd line, but I would challenge you to enter this reliably.

It will also remove any leading : in the source line.

 @ECHO OFF SETLOCAL IF %1 lss 0 (SET /a line=-%1) ELSE (SET /a line=%1) FOR /f "tokens=1* delims=:" %%a IN ('findstr /n "^" "%~2"') DO IF %%a leq %line% ECHO(%%b GOTO :EOF 

This batch saved as head.bat , located anywhere on your path, will allow you to use

 head -n standardwaffle.txt >junk.txt 

to extract the first lines of n from standardwaffle.txt to junk.txt

- will be optional

but this requires installing the package on your computer. Is this forbidden by your requirement “without installation” or “installation” means only for third-party utilities?

+2
source share
 Set Inp = WScript.Stdin Set Outp = Wscript.Stdout x = 0 Do Until Inp.AtEndOfStream x = x + 1 OutP.WriteLine Inp.Readline If x = 5 then Exit Do Loop 

Prints lines 1 through 5. To use

 cscript //nologo <path to script.vbs> <inputfile >outputfile 
+1
source share

To get the correct utf8 output, follow these steps in powershell

 chcp 65001 $OutputEncoding = New-Object -typename System.Text.UTF8Encoding get-content input.txt -encoding UTF8 |select-object -first 10000 > output.txt 

This will get the first 10,000 lines of input.txt (a file in utf8 format) in output.txt with the correct encoding.

+1
source share

you can use this:

 break>"%temp%\empty"&&fc "%temp%\empty" "%file_to_process%" /lb X /t |more +4 | findstr /B /E /V "*****" 

where you have to replace X with the lines you need. Enter the name head.bat :

 break>"%temp%\empty"&&fc "%temp%\empty" "%file_to_process%" /lb %~1 /t |more +4 | findstr /B /E /V "*****" 
0
source share

If you wanted to stick with simple Windows commands, you could use this, but it would be a bit slow for large files ;-) (below I added a second solution that works better :-), this retrieves the last 100 entries of any file length)

 find /n " " <test.txt >test.tmp for /l %%i in (1,1,100) do find "[%%i]" <test.tmp >test.tmp2 for /f "delims=] tokens=2" %%i in (test.tmp2) do echo %%i >>test.new del test.tmp del test.tmp2 move /y test.new test.txt find /v /n "" <test.txt >test.tmp for /f "delims=: tokens=2 %%i in ('find /v /c "" test.txt') do set /a NR=%%i set /a NS=%NR%-100 for /l %%i in (%NS%, 1, %NR%) do find "[%%i]" <test.tmp >>test.tmp2 for /f %%i "delims=] tokens=2 %%i in (test.tmp2) do echo %%i >>test.new move /y test.new test.txt 
0
source share

All Articles