Re: [ILUG] sed people ?

From: kevin lyda (kevin at domain suberic.net)
Date: Fri 11 Feb 2000 - 20:39:56 GMT


Kenn Humborg wrote:
> > sed 's/ *| */|/g' file | cat > file
> >
> > surely ??
> Is that _guaranteed_ not to truncate 'file' before sed
> reads it?

the short answer: no. your answer was right, > file1; mv file1 file

the long answer:

roo(8:02pm)~% cp /etc/group moo
roo(8:02pm)~% wc -c moo
    495 moo
roo(8:02pm)~% wc -c moo | cat > moo
roo(8:02pm)~% cat moo
    495 moo
roo(8:02pm)~% cp /etc/group moo
roo(8:02pm)~% wc -c moo > moo
roo(8:02pm)~% cat moo
      0 moo
roo(8:03pm)~% cp /etc/group moo
roo(8:04pm)~% (wc -c moo | cat) > moo
roo(8:04pm)~% cat moo
      0 moo

fascinating. that was with tcsh and bash was the same. i wouldn't
really trust it since i don't think there's anything in the shell specs
to say it should work that way. ah, it's a timing thing:

roo(8:07pm)~% cat foozle
#!/bin/sh
sleep 2
wc -c $1
roo(8:07pm)~% ./foozle moo | cat > moo
roo(8:08pm)~% cat moo
      0 moo

so if sed was to to start up, but not get to the point of opening the
fd, and then the shell was to be swapped back in to start cat, the file
would be lost. in fact according to the tcsh man page the file at the
receiving end of > is truncated if it exists (and noclobber isn't set,
if it is you'll need to do >!), so the sed command would need to read
the entire file - it isn't unlink()'d and creat()'d by the shell.

i exceeded my buffer cache here and it seems to have failed, though one
of one meg did fine:

roo(8:19pm)~% free
             total used free shared buffers
cached
Mem: 128080 125076 3004 14328 6092
18376
-/+ buffers/cache: 100608 27472
Swap: 105800 60580 45220
roo(8:19pm)~% dd if=/dev/zero of=moo bs=1024k count=40
40+0 records in
40+0 records out
roo(8:20pm)~% wc -c moo
41943040 moo
roo(8:21pm)~% sed s/0/1/g moo | cat > moo
roo(8:21pm)~% wc -c moo
      0 moo

interesting.

kevin

-- 
kevin at domain suberic.net                              Nutrition Facts
fork()'ed on 37058400		       Puns: 100% RDA  (% good puns: 0)


This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:05:24 GMT