non gnuwin32 alternative to head?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 227
Joined: 01 Aug 2011 13:48

non gnuwin32 alternative to head?

#1 Post by taripo » 15 Oct 2016 15:06

What methods are there for me to display the first n lines of a file, it seems there is a bug in piping to gnuwin32 head and gnuwin32 sed viewtopic.php?f=3&t=7480 (I can use them without pipe as then they won't run into the bug, but i'd like commands that I can pipe to)

So i'm interested in alternatives (ones where I can pipe input to them)

So let's say I have a file

Code: Select all

C:\ff\ff>for /L %f in (1,1,500) do @echo zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz >>testfile

And I want to display the first 3 lines of it and use pipe

Code: Select all

type testfile | head -n 3


I want some alternatives to gnuwin32 head, and not a gnuwin32 alternative.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: non gnuwin32 alternative to head?

#2 Post by Squashman » 15 Oct 2016 16:55

taripo wrote:I want some alternatives to gnuwin32 head, and not a gnuwin32 alternative.

Well that is a head scratcher.

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: non gnuwin32 alternative to head?

#3 Post by batchcc » 15 Oct 2016 17:16

Squashman wrote:
taripo wrote:I want some alternatives to gnuwin32 head, and not a gnuwin32 alternative.

Well that is a head scratcher.

Ha great pun. Well I better head off now.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: non gnuwin32 alternative to head?

#4 Post by aGerman » 16 Oct 2016 05:38

Just write your own batch tool if you don't want to use a 3rd party. E.g.

head.bat

Code: Select all

:: Syntax:
:: command|head.bat [number]

@echo off &setlocal EnableExtensions DisableDelayedExpansion
for /f "delims=" %%i in ('find /n /v ""') do (
  if "%~1" neq "" for /f "delims=[]" %%j in ("%%i") do if %%j gtr %~1 exit /b
  set "line=%%i"
  setlocal EnableDelayedExpansion
  echo(!line:*]=!
  endlocal
)


Code for testing

Code: Select all

:: !%<>|&()^?*"

:: "!%<>|&()^?*

@echo off &setlocal
type "%~f0"|head.bat 3
pause

Output:

Code: Select all

:: !%<>|&()^?*"

:: "!%<>|&()^?*
Drücken Sie eine beliebige Taste . . .

Steffen

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: non gnuwin32 alternative to head?

#5 Post by penpen » 16 Oct 2016 05:58

taripo wrote:it seems there is a bug in piping to gnuwin32 head and gnuwin32 sed viewtopic.php?f=3&t=7480
There's no bug, see http://www.dostips.com/forum/viewtopic.php?t=7480#p49689.


penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: non gnuwin32 alternative to head?

#6 Post by foxidrive » 20 Oct 2016 03:14

batchcc wrote:
Squashman wrote:
taripo wrote:I want some alternatives to gnuwin32 head, and not a gnuwin32 alternative.

Well that is a head scratcher.

Ha great pun.


I chuckled too. :)

Post Reply