UNIX pipes


(David Douthitt) #1

I thought I could do:

@snaptime = /usr/contrib/bin/gunzip -lv #{file} | /usr/bin/sed 's/^[^ ]* [^ ]* \(... .. ..:..\).*/\1/'.chomp
p @snaptime

…and it should (to me, anyway) return:

“Jun 12 13:54”

…instead of the odd result of:

“\001”

What gives? That’s not a returned error code, is it?

I tried “system()” but it doesn’t return a string the way I want, and it didn’t work anyway.

What’s the accepted way to use a UNIX pipe? I wound up using Ruby commands instead of sed…


(Rick Bradley) #2

I thought I could do:

@snaptime = /usr/contrib/bin/gunzip -lv #{file} | /usr/bin/sed 's/^[^ ]* [^ ]* \(... .. ..:..\).*/\1/'.chomp
p @snaptime

…and it should (to me, anyway) return:

“Jun 12 13:54”

…instead of the odd result of:

“\001”

Are your backslashes tripping you up?

Rick

···


http://www.rickbradley.com MUPRN: 481 (91F/100F)
> issues wrt using
random email haiku | someone else’s code so long
> as we’re allowed to.


(Ned Konz) #3

I thought I could do:

@snaptime = /usr/contrib/bin/gunzip -lv #{file} | /usr/bin/sed 's/^[^ ]* [^ ]* \(... .. ..:..\).*/\1/'.chomp p @snaptime

…and it should (to me, anyway) return:

“Jun 12 13:54”

…instead of the odd result of:

“\001”

What gives? That’s not a returned error code, is it?

No, it’s the \1 you asked for in the sed command…

Ruby quoting doesn’t work like shell quoting: single quotes inside
double quotes or backticks don’t mean anything special. So Ruby (not
sed) got the \1, and decided it meant octal 1 (\001).

···

On Thursday 13 June 2002 04:13 pm, David Douthitt wrote:

I tried “system()” but it doesn’t return a string the way I want,
and it didn’t work anyway.

What’s the accepted way to use a UNIX pipe? I wound up using Ruby
commands instead of sed…


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


(Dossy) #4

I thought I could do:

@snaptime = /usr/contrib/bin/gunzip -lv #{file} | /usr/bin/sed 's/^[^ ]* [^ ]* \(... .. ..:..\).*/\1/'.chomp
p @snaptime

…and it should (to me, anyway) return:

“Jun 12 13:54”

…instead of the odd result of:

“\001”

The \001 is probably happening because the \1 in your sed pattern is
getting turned into a \001 by Ruby, instead of getting passed to
sed as the string “\1” …

Try:

@snaptime = /usr/contrib/bin/gunzip -lv #{file} | /usr/bin/sed 's/^[^ ]* [^ ]* \(... .. ..:..\).*/\\1/'.chomp

Escape the \ of \1 as \1.

What’s the accepted way to use a UNIX pipe? I wound up using Ruby
commands instead of sed…

Why use sed when Ruby already has regex? I’d certainly just take the
output of gunzip and process it in Ruby rather than with another
process (sed).

– Dossy

···

On 2002.06.14, David Douthitt DDouthitt@cuna.coop wrote:


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)


(Yukihiro Matsumoto) #5

HI,

@snaptime = /usr/contrib/bin/gunzip -lv #{file} | /usr/bin/sed 's/^[^ ]* [^ ]* \(... .. ..:..\).*/\1/'.chomp
p @snaptime

…and it should (to me, anyway) return:

“Jun 12 13:54”

…instead of the odd result of:

“\001”

What gives? That’s not a returned error code, is it?

`` eats your backslashes, so that it passes sed

( -> (
) -> )
\1 -> \001

which are not what you want. Try

@snaptime = /usr/contrib/bin/gunzip -lv #{file} | /usr/bin/sed 's/^[^ ]* [^ ]* \\(... .. ..:..\\).*/\\1/'.chomp

						matz.
···

In message “UNIX pipes” on 02/06/14, “David Douthitt” DDouthitt@cuna.coop writes:


(Ned Konz) #6

Why use gunzip when Ruby has Ruby/zlib?
http://www.blue.sky.or.jp/atelier/ruby/
http://www.blue.sky.or.jp/atelier/ruby/ruby-zlib-0.5.1.tar.gz

···

On Thursday 13 June 2002 05:27 pm, Dossy wrote:

Why use sed when Ruby already has regex? I’d certainly just take
the output of gunzip and process it in Ruby rather than with
another process (sed).


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE