views:

1233

answers:

7

Is there a way to simulate the *nix head command in DOS? I have a file and I want a way to stip off the first n-lines of text. For example:

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

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

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).

EricSchaefer
+1  A: 

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

Zian Choy
+3  A: 

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

It, among other things, contains head.

Sebastian P.
A: 

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

JeffH
+3  A: 

The native DOS command "more" has a +n option that will start outputting the file after the nth line:

more +2 myfile.txt

Will start outputting at line 3. This is actually the inverse of Unix head:

head -2 myfile.txt

will print the first 2 lines, whereas

more +2 myfile.txt

will print everything after the first two lines.

Matt Nizol
+2  A: 

When using more +n that Matt already mentioned, to avoid pauses in long files, try this:

more +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.)

Anon
+1  A: 

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.

brianary