Windows equivalent of the 'tail' command

前端 未结 21 2845
长情又很酷
长情又很酷 2020-11-28 01:48

Is there a way to simulate the *nix tail command on the Windows command line? I have a file and I want a way to snip off the first n lines of text. For example:

相关标签:
21条回答
  • 2020-11-28 02:28

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

    0 讨论(0)
  • 2020-11-28 02:29

    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.

    0 讨论(0)
  • 2020-11-28 02:30

    No exact equivalent. However there exist a native DOS command "more" that has a +n option that will start outputting the file after the nth line:

    DOS Prompt:

    C:\>more +2 myfile.txt
    

    The above command will output everything after the first 2 lines.
    This is actually the inverse of Unix head:

    Unix console:

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

    The above command will print only the first 2 lines of the file.

    0 讨论(0)
  • 2020-11-28 02:30

    You could get CoreUtils from GnuWin32, which is a collection of standard unix tools, ported to Windows.

    It, among other things, contains head.

    0 讨论(0)
  • 2020-11-28 02:31

    This is a total hack but if it's a huge file that you want to just examine the format, header, etc. and you're looking for a solution you can always just redirect the 'more' output to a new file and CTRL-C quickly. The output rows can't be controlled precisely and you will most likely kill it in the middle of a line of output but it's a cheap way of grabbing a small bit of an otherwise unusable file.

    Ex.

    C:\more test.csv > test.txt 
    ^C C:\more test.txt
    line 1
    line 2
    etc...... C:\
    0 讨论(0)
  • 2020-11-28 02:31

    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.

    0 讨论(0)
提交回复
热议问题