Re: Standard output piping problem

From: Netocrat (netocrat_at_dodo.com.au)
Date: 05/29/05


Date: Sat, 28 May 2005 22:00:08 GMT

Alan Connor wrote:

> On comp.unix.shell, in <4298d592@news.comindico.com.au>,
> "Netocrat" wrote:
>
>> Alan Connor wrote:
>>
>>> <snip>
>>>
>>> I played with it a bit...
>>>
>>> #!/bin/sh
>>>
>>> while true ; do /usr/sbin/pppstats -c 1 | sed '/IN/d' | \
>>>
>>> ......
>>>
>>> sleep 1 ; done
>>>
>>>
>>> HTH
>>>
>>> AC
>>
>> There are two problems with that: 1) I wanted to avoid running
>> multiple pppstats processes - it offers continuous output so
>> I'd prefer to take advantage of that.
>
> Why do you want to avoid multiple processes that just die in
> a blink?

Call me a perfectionist if you will but I'd rather avoid the overhead of
unecessary process creation.

> You'll be calling many of them in the script itself...

Actually I don't plan on doing that.
 
> 2) On all but the first
>> output, pppstats gives differential stats which saves me having
>> to perform arithmetic since that's the data I'm after.
>>
>> Let me come clean on my full approach: I'm trying to generate
>> an input stream in the form of a series of lines, each with
>> two numbers separated by a space. The first number is bytes
>> received over the interval and the second is bytes sent.
>>
>> So basically this script achieves it: #!/bin/sh
>> /usr/sbin/pppstats -w 1 | sed "{s/\([[:space:]]*\([0-9]*\).*
>> [[:space:]]*\([0-9]*\).*\)/\2 \3/}"
>
> That doesn't work.

My composer cut off a pipe character in the middle of the sed script. Try
this instead:

#!/bin/sh
/usr/sbin/pppstats -w 1 |
sed "s/\([[:space:]]*\([0-9]*\).*|[[:space:]]*\([0-9]*\).*\)/\2 \3/"

I get the following output:

262631059 180722327
4132 3978
1776 144
1259 1624
382 624
0 0
1278 200
468 1604
1548 814
1236 40
543 1636
1584 144
2766 1348
5561 1990
2144 140
3434 1740
1560 52
2687 204
3056 866
1878 1708

3265 208
2741 156

Note that the first line is blank and that there is a blank line after about
20 lines. These I wanted to remove by piping through:
grep -v ^[[:space:]]$

>> Cool, but the problems are (1) I have to ignore the first line
>> since it isn't a differential - no big deal (2) Occasionally
>> pppstats regenerates headers presumably based on its
>> understanding of when a new page occurs on your terminal. This
>> results in a blank line (actually a line comprised solely of a
>> space). No big deal, I thought, I'll filter it out with grep.
>> However as you can see in my reply to Barry, grep piped to sed
>> or vice versa results in no output.
>
> Yes. I know. That's why I came up with the solution I did.
>
>> So that's the real issue.
>> Where in the pipes is the stream blocking?
>>
>
> Don't know. You can pipe it to Cat -A and the output is just
> what one would expect.

Not for my script. Let's call the script above pppmon. Now with:
$ pppmon | cat -A

I get no results.

> I'm thinking it has to do with null-terminated lines and
> that printf might solve the problem.

Yes my thoughts were about perhaps line termination problems too. How do
you propose to use printf to solve the problem?

> Why do you enclose the sed scripts in {} ?

>From the manpage:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...

That seems to indicate it's required. It seems to work without it, but I
took it from the manpage.



Relevant Pages

  • Re: fileevent and vwait dilema with one tcl script calling multiple copies of another
    ... pies of each script. ... loop *IS* the event loop and the 'main program' has no loop at all. ... # Event proc: file read event. ... proc Reader {pipe} { ...
    (comp.lang.tcl)
  • accessing a pipe from TCL and C, hang in `close/exit
    ... I have a TCL shell script which opens a socket to some remote host, ... and opens a read-converter pipe. ... data arrives on the remote host. ... Now I want to close the channel from C++: ...
    (comp.lang.tcl)
  • I can process my mail!
    ... I figured out how to pipe mail to my own scripts! ... the script as an argument. ... that won't confuse boundaries for message text. ... sendmail, but I'm not that interested in sendmail in particular. ...
    (comp.unix.questions)
  • Re: how do i..
    ... On Sun, 2004-08-22 at 18:31, Mr. Oberoi wrote: ... > error results from the program(shell script) to a file. ... scriptname> filename 2>&1 ... the pipe character is used to "chain" the output of the command on ...
    (Fedora)
  • Re: IO::Pipe and loss of data
    ... May be I cut too much, but as the last run of my big script was ... > show at least how I work to send a message and use the pipe. ... There is a fixed amount of buffer (probably 4K ... for each "server". ...
    (comp.lang.perl.misc)