(sed 1q ; sed 2q) : no output from 2nd 'sed'

(sed 1q ; sed 2q) : no output from 2nd 'sed'

Post by Nicolas TRIPO » Sat, 31 Oct 1998 04:00:00



  In the two examples below, why does the second 'sed' produce no output when
the second 'dd' does : ?

$ echo '0
1
2
3
4' | (sed 1q ; sed 2q)
0
$

$ echo '0
1
2
3
4' | (dd bs=1 count=2 ; dd bs=1 count=4)
0
2+0 records in
2+0 records out
1
2
4+0 records in
4+0 records out
$

 
 
 

(sed 1q ; sed 2q) : no output from 2nd 'sed'

Post by Stephane CHAZEL » Sat, 31 Oct 1998 04:00:00



>  In the two examples below, why does the second 'sed' produce no output when
>the second 'dd' does : ?

>$ echo '0
>1
>2
>3
>4' | (sed 1q ; sed 2q)
>0
>$

>$ echo '0
>1
>2
>3
>4' | (dd bs=1 count=2 ; dd bs=1 count=4)
>0
>2+0 records in
>2+0 records out
>1
>2
>4+0 records in
>4+0 records out
>$

yes | nl | (sed 1q;sed 5q)
     1  y
 911    y
   912  y
   913  y
   914  y
   915  y

Sed doesn't read a character at a time but reads larges buffers. You can
check it with:
yes | strace sed 1q
[...]
read(0, "y\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\n"..., 4096) = 4096
                                                ^^^^
[...]

--
Stphane

 
 
 

(sed 1q ; sed 2q) : no output from 2nd 'sed'

Post by Bruce Barnet » Sat, 31 Oct 1998 04:00:00



>   In the two examples below, why does the second 'sed' produce no output when
> the second 'dd' does : ?

The difference is between

        (echo 0; echo 1; echo 2; echo 3; echo 4) | ( sed 1q; cat )
and
        (echo 0; echo 1; echo 2; echo 3; echo 4) | ( dd bs=1 count=2; cat )

The first one prints out the first number, while the second prints
all of them.

I would think that when sed quits, it reads the rest of standard input
until it's empty.  The command dd, however, does not consume the rest
of STDIN.  I tested these, which also act like sed: (consumes)

        ( awk 'NR==1';cat)
        ( head -1;cat)
        ( sed -n 1p;cat)

The following act like dd: (non-consumes)

        (perl -e '(<>) && exit;';cat)
        (sh -c 'read line'; cat)
        (ksh -c 'read line'; cat)

csh can't be used for this test, because $< only reads from the
terminal, not STDIN, and csh doesn't have a mechanism to read one
line.

Do I get a UUOC? I used cat with NO arguments. That's MUCH WORSE than
using one.    :-)

--
Bruce  <barnett at crd. ge. com> (speaking as myself, and not a GE employee)

 
 
 

1. sed sed sed

On solaris 9 - why are there multiple copies of sed installed.  One in /usr/ucb
owned by the SUNWscpu third party package, /usr/xpg4/bin/sed by SUNWxcu4, and
one in /usr/bin owned by SUNWcsu.  I've looked on other Sun machines not
maintained by me and the same versions of sed are in the same place owned by the
same packages.

Why the multiple copies?

2. Trouble compiling programs with Infomagic Moo-tiff

3. . 2 sed FAQs, sed exes, sed cetras; URLs

4. SOS linux networking

5. SED SED SED...

6. Ethics

7. sed -e 's/\'a\'/\';\'/' ?

8. Advice on partitioning requested ....

9. Simple 'sed', 'awk', 'cut' problem

10. How do I use Sed to replace pipe '|' with ','

11. sed and '/' to '\/' conversion.

12. what's awk(1)'s equivalent of sed(1)'s `!d' flag?

13. PROBLEM: 'sed' script 's/^ /\n/' not working properly