Extract N lines from file using single windows com

2019-01-18 13:12发布

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

6条回答
乱世女痞
2楼-- · 2019-01-18 13:24

You can use PowerShell from the cmd.exe console:

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

You could 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}"

Usage:

head input.txt 10 >output.txt

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

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

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.

查看更多
一夜七次
3楼-- · 2019-01-18 13:26

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

N - number of lines.

From the begining of file:

Get-Content -Head N file.txt

From the end of file:

Get-Content -Tail N file.txt
查看更多
放我归山
4楼-- · 2019-01-18 13:30

you can use this:

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

where you should replace the X with the lines you want.Or name this head.bat :

break>"%temp%\empty"&&fc "%temp%\empty" "%file_to_process%" /lb  %~1 /t |more +4 | findstr /B /E /V "*****"
查看更多
看我几分像从前
5楼-- · 2019-01-18 13:32
(@FOR /f "tokens=1* delims=:" %a IN ('findstr /n "^" "standardwaffle.txt"') DO @IF %a leq 7 ECHO(%b)>u:\junk.txt

would extract the first 7 lines of standardwaffle.txt to u:\junk.txt so here it is in one cmd line - but I'd defy you to enter that reliably.

It would also remove any leading : on a 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 placed anywhere on your path would allow you to use

head -n standardwaffle.txt >junk.txt

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

the - would be optional

but 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?

查看更多
闹够了就滚
6楼-- · 2019-01-18 13:34
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

This prints lines 1 to 5. To use

cscript //nologo <path to script.vbs> <inputfile >outputfile
查看更多
该账号已被封号
7楼-- · 2019-01-18 13:41

In order to get correct utf8 output, do the following 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 first 10000 lines of input.txt (file in utf8 format) to output.txt with correct encoding.

查看更多
登录 后发表回答