Re: How to copy n bytes from stdin to stdout?

2018-06-28 Thread Steve Litt
On Wed, 27 Jun 2018 10:06:40 -0700 xi wrote: > > On Jun 25, 2018, at 16:19, Tomasz Rola wrote: > > > > On Sun, Jun 24, 2018 at 10:53:37PM -0400, Steve Litt wrote: > >> On Thu, 21 Jun 2018 00:56:04 +0200 > >> Tomasz Rola wrote: > >> > > [...] > >>> Craps. I have consulted OpenBSD's manpa

Re: How to copy n bytes from stdin to stdout?

2018-06-27 Thread xi
> On Jun 25, 2018, at 16:19, Tomasz Rola wrote: > > On Sun, Jun 24, 2018 at 10:53:37PM -0400, Steve Litt wrote: >> On Thu, 21 Jun 2018 00:56:04 +0200 >> Tomasz Rola wrote: >> > [...] >>> Craps. I have consulted OpenBSD's manpage for dd and there is no >>> mention of iflag. So this will not work

Re: How to copy n bytes from stdin to stdout?

2018-06-25 Thread Theo de Raadt
Tomasz Rola wrote: > On Sun, Jun 24, 2018 at 10:53:37PM -0400, Steve Litt wrote: > > On Thu, 21 Jun 2018 00:56:04 +0200 > > Tomasz Rola wrote: > > > [...] > > > Craps. I have consulted OpenBSD's manpage for dd and there is no > > > mention of iflag. So this will not work on OpenBSD. I will have

Re: How to copy n bytes from stdin to stdout?

2018-06-25 Thread Tomasz Rola
On Sun, Jun 24, 2018 at 10:53:37PM -0400, Steve Litt wrote: > On Thu, 21 Jun 2018 00:56:04 +0200 > Tomasz Rola wrote: > [...] > > Craps. I have consulted OpenBSD's manpage for dd and there is no > > mention of iflag. So this will not work on OpenBSD. I will have to > > rethink this, sorry. > > >

Re: How to copy n bytes from stdin to stdout?

2018-06-24 Thread Steve Litt
On Thu, 21 Jun 2018 00:56:04 +0200 Tomasz Rola wrote: > On Thu, Jun 21, 2018 at 12:44:14AM +0200, Tomasz Rola wrote: > [...] > > => (591 60): cat nread > > #!/bin/sh > > > > # nread n - read up to n bytes from stdio, put them on to stdout > > > > N=$1 > > > > dd bs=512 count=$((N / 512))

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Otto Moerbeek
On Thu, Jun 21, 2018 at 08:06:46PM +0200, Vincent Legoll wrote: > Hello, > > The man page did not say bs has to be a power of 2. > > On a very old macppc openbsd box: > > vince@mini:~$ dd count=1 bs=123456789 < /dev/zero > zero.bin > 1+0 records in > 1+0 records out > 123456789 bytes transferre

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Vincent Legoll
Hello, The man page did not say bs has to be a power of 2. On a very old macppc openbsd box: vince@mini:~$ dd count=1 bs=123456789 < /dev/zero > zero.bin 1+0 records in 1+0 records out 123456789 bytes transferred in 9.833 secs (12554493 bytes/sec) On a much more recent core i7 linux: vince@del

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Maximilian Pichler
On Thu, Jun 21, 2018 at 3:15 PM, Daniel Hartmeier wrote: > I think they meant dd and just didn't care about efficiency: > > http://austingroupbugs.net/bug_view_page.php?bug_id=407 Thanks for digging this out! > Does ghead -c beat a simple buffer loop? Your head-c.c seems to have the same perf

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Otto Moerbeek
On Thu, Jun 21, 2018 at 03:08:10PM +0200, Jan Stary wrote: > On Jun 20 17:32:51, maxim.pich...@gmail.com wrote: > > dd bs=1 count=1234567 will copy 1234567 bytes and then stop, but it's slow. > > $ dd bs=1 count=1234567 < /dev/zero > /dev/null > 1234567+0 records in > 1234567+0 records out > 1234

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Daniel Hartmeier
On Thu, Jun 21, 2018 at 03:08:10PM +0200, Jan Stary wrote: > $ dd count=1 bs=1234567 < /dev/zero > /dev/null > 1+0 records in > 1+0 records out > 1234567 bytes transferred in 0.001 secs (653970943 bytes/sec) That was my first hunch as well, but try $ printf "foo\nbar\n" | dd count=1 bs=1234

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Daniel Hartmeier
On Thu, Jun 21, 2018 at 10:57:40AM +0200, Maximilian Pichler wrote: > > dd ibs=1 count=n > > Nice, this is about three time as fast as bs=1. Both are much slower > than 'ghead -c'. I think they meant dd and just didn't care about efficiency: http://austingroupbugs.net/bug_view_page.ph

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Jan Stary
On Jun 20 17:32:51, maxim.pich...@gmail.com wrote: > dd bs=1 count=1234567 will copy 1234567 bytes and then stop, but it's slow. $ dd bs=1 count=1234567 < /dev/zero > /dev/null 1234567+0 records in 1234567+0 records out 1234567 bytes transferred in 4.507 secs (273886 bytes/sec) $ dd count=1 bs=12

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Maximilian Pichler
On Thu, Jun 21, 2018 at 11:05 AM, Abel Abraham Camarillo Ojeda wrote: > $ cat file | perl -ne 'BEGIN { $/ = \1 } print if $. <= 5; exit 0 if $. == 5' This is much slower than 'dd ibs=1'

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Abel Abraham Camarillo Ojeda
On Thu, Jun 21, 2018 at 3:57 AM, Maximilian Pichler wrote: > On Thu, Jun 21, 2018 at 9:48 AM, Otto Moerbeek wrote: >>> On Thu, Jun 21, 2018 at 12:11:52AM +0200, Maximilian Pichler wrote: >>> > I'm just wondering what these other utilities might be. >>> >>> hexdump -v -n 1234567 -e '"%c"' > > Spee

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Maximilian Pichler
On Thu, Jun 21, 2018 at 9:48 AM, Otto Moerbeek wrote: >> On Thu, Jun 21, 2018 at 12:11:52AM +0200, Maximilian Pichler wrote: >> > I'm just wondering what these other utilities might be. >> >> hexdump -v -n 1234567 -e '"%c"' Speed-wise this is roughly on par with 'dd bs=1'. >> If the input doesn'

Re: How to copy n bytes from stdin to stdout?

2018-06-21 Thread Otto Moerbeek
On Thu, Jun 21, 2018 at 07:38:49AM +0200, Daniel Hartmeier wrote: > On Thu, Jun 21, 2018 at 12:11:52AM +0200, Maximilian Pichler wrote: > > > I'm just wondering what these other utilities might be. > > hexdump -v -n 1234567 -e '"%c"' > > If the input doesn't contain backslashes (or something el

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Daniel Hartmeier
On Thu, Jun 21, 2018 at 12:11:52AM +0200, Maximilian Pichler wrote: > I'm just wondering what these other utilities might be. hexdump -v -n 1234567 -e '"%c"' If the input doesn't contain backslashes (or something else, tr(1)) vis -aoF6 | head -n 1234567 | unvis Daniel

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Tomasz Rola
On Thu, Jun 21, 2018 at 12:44:14AM +0200, Tomasz Rola wrote: [...] > => (591 60): cat nread > #!/bin/sh > > # nread n - read up to n bytes from stdio, put them on to stdout > > N=$1 > > dd bs=512 count=$((N / 512)) iflag=fullblock 2>/dev/null > dd bs=1 count=$((N % 512)) iflag=fullblock 2>/de

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Tomasz Rola
On Thu, Jun 21, 2018 at 12:02:14AM +0200, Maximilian Pichler wrote: > Your script is incorrect. > > $ dd if=/dev/zero bs=1 | ./nread 1234567 | wc -c > 0+2411 records in > 0+2411 records out > 2411 bytes transferred in 0.038 secs (62579 bytes/sec) > 135+0 records in > 135+0 records out > 135 bytes

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Maximilian Pichler
POSIX says (http://pubs.opengroup.org/onlinepubs/9699919799/utilities/head.html#tag_20_57_18) about the 'head' utility: "There is no -c option (as there is in tail) because it is not historical practice and because other utilities in this volume of POSIX.1-2017 provide similar functionality." I'

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Maximilian Pichler
Your script is incorrect. $ dd if=/dev/zero bs=1 | ./nread 1234567 | wc -c 0+2411 records in 0+2411 records out 2411 bytes transferred in 0.038 secs (62579 bytes/sec) 135+0 records in 135+0 records out 135 bytes transferred in 0.001 secs (126148 bytes/sec) 2546 $ man dd ... count=n Copy

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Tomasz Rola
On Wed, Jun 20, 2018 at 08:20:16PM +0200, Maximilian Pichler wrote: > On Wed, Jun 20, 2018 at 7:17 PM, Tomasz Rola wrote: > > But seriously: man sh. > > Are you saying there is a shell built-in that does this? If so, which one? => (591 13):cat nread #!/bin/sh # nread n - read up to n bytes

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Maximilian Pichler
On Wed, Jun 20, 2018 at 7:17 PM, Tomasz Rola wrote: > But seriously: man sh. Are you saying there is a shell built-in that does this? If so, which one?

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Tomasz Rola
On Wed, Jun 20, 2018 at 06:50:06PM +0200, Tomasz Rola wrote: [...] > > Maybe suggest to the teacher that she sticks to numbers divisible by > 512? That is what haxorz do. But seriously: man sh. -- Regards, Tomasz Rola -- ** A C programmer asked whether computer had Buddha's nature. ** **

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Tomasz Rola
On Wed, Jun 20, 2018 at 06:35:49PM +0200, Maximilian Pichler wrote: > On Wed, Jun 20, 2018 at 6:27 PM, Tomasz Rola wrote: > > On my Linux box: > > ? > > > cat HUGE | /usr/bin/time dd bs=1 count=1234944 | wc -c > > stdin might be something much faster than your disk, in which case the > relative

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Maximilian Pichler
On Wed, Jun 20, 2018 at 6:27 PM, Tomasz Rola wrote: > On my Linux box: ? > cat HUGE | /usr/bin/time dd bs=1 count=1234944 | wc -c stdin might be something much faster than your disk, in which case the relative cost of bs=1 increases. > cat HUGE | /usr/bin/time dd bs=1024 count=1206 | wc -c Do

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Tomasz Rola
On Wed, Jun 20, 2018 at 06:01:21PM +0200, Maximilian Pichler wrote: > On Wed, Jun 20, 2018 at 5:50 PM, Solene Rapenne wrote: > > it's slow because it flushes the output every byte, what would you > > expect? Maybe you should do in a different manner. > > I know, my question is what such a differe

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Maximilian Pichler
On Wed, Jun 20, 2018 at 5:50 PM, Solene Rapenne wrote: > it's slow because it flushes the output every byte, what would you > expect? Maybe you should do in a different manner. I know, my question is what such a different manner might look like. :)

Re: How to copy n bytes from stdin to stdout?

2018-06-20 Thread Solene Rapenne
Maximilian Pichler writes: > dd bs=1 count=1234567 will copy 1234567 bytes and then stop, but it's slow. > > I can't seem to think of a faster command that also works in the > presence of short reads and blocking. There is ghead -c from coreutils > in ports, but this should be possible in base,

How to copy n bytes from stdin to stdout?

2018-06-20 Thread Maximilian Pichler
dd bs=1 count=1234567 will copy 1234567 bytes and then stop, but it's slow. I can't seem to think of a faster command that also works in the presence of short reads and blocking. There is ghead -c from coreutils in ports, but this should be possible in base, no? Max