development

'tail'명령에 해당하는 Windows

big-blog 2020. 8. 15. 09:53
반응형

'tail'명령에 해당하는 Windows


Windows 명령 줄에서 * nix tail 명령 을 시뮬레이션하는 방법이 있습니까? 파일이 있고 텍스트 의 처음 n 줄을 잘라내는 방법을 원합니다 . 예를 들면 :

D:\>type file.txt
line one
line two
line three
D:\>*[call to tail]* > result.txt

D:\>type result.txt
line two
line three

정확히 동등한 것은 없습니다. 그러나 n 번째 줄 이후에 파일 출력을 시작하는 + n 옵션이있는 기본 DOS 명령 "more"가 있습니다.

DOS 프롬프트 :

C:\>more +2 myfile.txt

위의 명령은 처음 두 줄 이후의 모든 것을 출력합니다.
이것은 실제로 Unix head 의 반대입니다 .

Unix 콘솔 :

root@server:~$ head -2 myfile.txt

위의 명령은 파일의 처음 두 줄만 인쇄합니다.


경우 당신이 윈도우 PowerShell을 설치 방금 cmd.exe를에서 실행할 수 있습니다 (I는 XP 이후 포함 생각)

헤드 명령 :

powershell -command "& {Get-Content *filename* -TotalCount *n*}"

꼬리 명령 :

powershell -command "& {Get-Content *filename* | Select-Object -last *n*}"

또는 PowerShell에서 직접 :

Get-Content *filename* -TotalCount *n*
Get-Content *filename* | Select-Object -last *n*


최신 정보

PowerShell 3.0 (Windows 8 이상) Tail은 alias Last. HeadFirst별칭 TotalCount도 추가되었습니다.

따라서 명령은 다음과 같이 다시 작성할 수 있습니다.

Get-Content *filename* -Head *n*
Get-Content *filename* -Tail *n*

more /e filename.txt P n

여기서 n은 표시 할 행 수입니다. 빠르게 작동하며 head명령 과 똑같습니다 .


Windows로 포팅 된 표준 유닉스 도구 모음 인 GnuWin32 에서 CoreUtils얻을 수 있습니다.

무엇보다도 머리가 포함되어 있습니다.


이것은 완전한 해킹이지만 형식, 헤더 등을 검사하고 싶은 거대한 파일이고 해결책을 찾고 있다면 항상 'more'출력을 새 파일로 리디렉션하고 CTRL-C 빨리. 출력 행은 정확하게 제어 할 수 없으며 출력 행 중간에서 종료 할 가능성이 높지만 사용할 수없는 작은 파일을 잡는 저렴한 방법입니다.

전의.

C : \ more test.csv> test.txt 
^ C C : \ more test.txt
라인 1
라인 2
등 ...... 씨:\

Matt가 이미 언급 한 + n을 더 많이 사용하는 경우 긴 파일에서 일시 중지를 방지하려면 다음을 시도하십시오.

더 많은 +1 myfile.txt> con

When you redirect the output from more, it doesn't pause - and here you redirect to the console. You can similarly redirect to some other file like this w/o the pauses of more if that's your desired end result. Use > to redirect to file and overwrite it if it already exists, or >> to append to an existing file. (Can use either to redirect to con.)


Well, this will do it, but it's about as fast as it looks (roughly O(n*m), where n is the number of lines to display and m is the total number of lines in the file):

for /l %l in (1,1,10) do @for /f "tokens=1,2* delims=:" %a in ('findstr /n /r "^" filename ^| findstr /r "^%l:"') do @echo %b

Where "10" is the number of lines you want to print, and "filename" is the name of the file.


Powershell:

Get-Content C:\logs\result.txt -Tail 10

Get-Content C:\logs\result.txt -wait (monitor the tail)

you can also use Git bash where head and tail are emulated as well


Get-content -Tail n file.txt with powershell is the only thing that comes close to tail in linux.

The Get-Content *filename* | Select-Object -last *n* suggested above loads/parse the whole thing. Needless to say, it was not happy with my 10GB log file... The -Tail option does start by the end of the file.


If you want the head command, one easy way to get it is to install Cygwin. Then you'll have all the UNIX tools at your disposal.

If that isn't a good solution, then you can try using findstr and do a search for the end-of-line indicator.

findstr on MSDN: http://technet.microsoft.com/en-us/library/bb490907.aspx


There is a resource kit that can be downloaded from here: http://www.microsoft.com/downloads/en/confirmation.aspx?familyId=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displayLang=en

It contains a tail.exe tool but it is only compatible with some Windows versions

(Copied from this post: Tail command for windows)


FWIW, for those just needing to snip off an indeterminate number of records from the head of the file, more > works well. This is useful just to have a smaller file to work with in the early stages of developing something.


I have not tried extracting a range, but I was able to get a line using the following DOS command:

find /N " " *.log|find "[6]" 

Since most files contain spaces, this command pulls every line from all LOG files and basically numbers them starting from 1 for each file. The numbered results are then piped into the second FIND command which looks for the line tagged as number 6.


Here is a fast native head command that gives you the first 9 lines in DOS.

findstr /n "." myfile.txt | findstr "^.:"

The first 2 characters on each line will be the line number.


There's a free head utility on this page that you can use. I haven't tried it yet.


in PS try to use command:

Select -Last 1

This command can be pipelined also.

Example to get first line:

type .\out.txt | Select -Last 1

or to get the first line:

 type .\out.txt | Select -First 1

set /p line= < file.csv 
echo %line%

it will return first line of your file in cmd Windows in variable %line%.


As a contemporary answer, if running Windows 10 you can use the "Linux Subsystem for Windows".

https://docs.microsoft.com/en-us/windows/wsl/install-win10

This will allow you to run native linux commands from within windows and thus run tail exactly how you would in linux.


I don't think there is way out of the box. There is no such command in DOS and batch files are far to limited to simulate it (without major pain).


Warning, using the batch file for, tokens, and delims capability on unknown text input can be a disaster due to the special interpretation of chars like &, !, <, etc. Such methods should be reserved for only predictable text.

참고URL : https://stackoverflow.com/questions/1295068/windows-equivalent-of-the-tail-command

반응형