Is there a way to extract/copy the fist X number of lines from a file and input them into another file with a single command using the windows command prompt?
I can delete the first X number of lines using:
more +X [file_containing data] > [file_to_export_data_to]
If the head command would work I think I could just do this:
head -X [file_containing data] > [file_to_export_data_to]
But that unfortunately does not work.
It would be great if Windows had a "less" command but again no luck.
I'm a complete novice when it comes to this stuff so I'm sure I'm missing something obvious. I don't want to install anything or use something other than the command prompt.
Thanks
You can use PowerShell from the cmd.exe console:
You could create a DOSKEY macro to make it easier to use from the command line:
Usage:
But you cannot use a DOSKEY macro within a batch script.
You could create a head.bat script instead and place it in a folder that is included in your PATH:
head.bat
From the command line, you would use
head input.txt 10 >output.txt
From within a batch script, you would use
call head input.txt 10 >output.txt
I chose not to have the output file as a parameter in case you want to simply display the result to the screen instead of writing to a file.
the simplest one-command solution is to use Powershell Get-Content.
N - number of lines.
From the begining of file:
From the end of file:
you can use this:
where you should replace the X with the lines you want.Or name this
head.bat
:would extract the first 7 lines of
standardwaffle.txt
tou:\junk.txt
so here it is in onecmd
line - but I'd defy you to enter that reliably.It would also remove any leading
:
on a source line.This batch, saved as
head.bat
placed anywhere on your path would allow you to useto extract the first
n
lines ofstandardwaffle.txt
tojunk.txt
the
-
would be optionalbut this involves installing the batch on your machine. Is that banned by your "no installing" requirement, or is "installing" meant only for 3rd party utilities?
This prints lines 1 to 5. To use
In order to get correct utf8 output, do the following in powershell
This will get first 10000 lines of input.txt (file in utf8 format) to output.txt with correct encoding.