Awk equivalent in ruby

HI
how to use awk equivalent in ruby

in bash i can use , awk to get the value 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.49.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.48.47.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.48.47.48.47.49.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.49.47.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.49.47.48.47.49.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.49.46.51.50.55.54.56
= INTEGER: 1

but in ruby , how can i get the value 1 ??

Am 05.09.2012 21:19, schrieb Ferdous ara:

HI
how to use awk equivalent in ruby

in bash i can use , awk to get the value 1

how?

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.49.47.48.47.49.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.48.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.49.46.51.50.55.54.56

= INTEGER: 1

which ‘1’ ???

but in ruby , how can i get the value 1 ??

  • puts 1 :slight_smile:

  • google for ‘ruby regular expressions’
    (especially capturing might be useful)

which ‘1’ ???

i just need 1 thats it

  • google for ‘ruby regular expressions’
    S> (especially capturing might be useful)

I am reading in google. but very new in ruby …
so bit confused … ,
if you can give me some example that would be good
Thanks

Am 05.09.2012 21:57, schrieb Ferdous ara:

which ‘1’ ???

i just need 1 thats it

???

and: what would you do in awk?

  • google for ‘ruby regular expressions’
    S> (especially capturing might be useful)

I am reading in google. but very new in ruby …
so bit confused … ,
if you can give me some example that would be good
Thanks

1.9.3-p194 :001 > string = ‘a string with 27 characters’
=> “a string with 27 characters”
1.9.3-p194 :002 > /(?\d+) characters/ =~ string
=> 14
1.9.3-p194 :003 > number
=> “27”

see http://www.ruby-doc.org/core-1.9.3/Regexp.html

look for ‘named capture groups’

or for simple cases:

title = ‘Snow White and the 7 Dwarfs’
title[/\d+/] # => “7”

Ferdous ara wrote in post #1074829:

HI
how to use awk equivalent in ruby

in bash i can use , awk to get the value 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.48.46.51.50.55.54.56

= INTEGER: 1

As a one-liner, awk-style?

echo “Foo.1.2.3 = INTEGER: 1” | ruby -ane ‘puts $F[3]’

In a ruby script?

str = “Foo.1.2.3 = INTEGER: 1”
a, b, c, d = str.split
puts d

This is assuming the ‘1’ you want is the one at the end of the line.

Josh C. wrote in post #1075006:

Anyway, to expand on Brian’s answer:

$ cat f

FC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.48.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.49.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.48.47.48.47.48.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.48.47.48.47.49.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.49.47.48.47.48.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.49.47.48.47.49.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.48.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.49.46.51.50.55.54.56

= INTEGER: 1

$ ruby -ane ‘puts $F[2] if /^= INTEGER: /’ < f
1
1
1
1
1
1
1
1

Josh has assumed that these are pairs of lines. I think this was just an
artefact of formatting by the OP’s mail client, and they are really
single lines. In that case you just want the 4th column of each line:

ruby -ane ‘puts $F[3]’ < f

On Fri, Sep 7, 2012 at 4:56 AM, Brian C. [email protected]
wrote:

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.49.46.51.50.55.54.56

1

Oh, you’re probably right. This was submitted via Ruby Forum, which does
wrap lines IIRC. No wonder the data was so confusing >.<

Is this a setting someone could turn off? I think it is objectively
incorrect to wrap lines for people. Their display software (in my case,
Gmail) should be responsible for determining the presentation settings
(line width), not the input source. The input source (Ruby Forum) has no
idea, and is making incorrect assumptions about my display, which is
causing problems.

On Wed, Sep 5, 2012 at 2:19 PM, Ferdous ara [email protected]
wrote:

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.48.46.51.50.55.54.56

= INTEGER: 1

RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.49.46.51.50.55.54.56

= INTEGER: 1

This question was asked in a very confusing way, I didn’t realize = INTEGER: 1 was part of the input, I thought it was the result you
expected
to get after doing some sort of analysis on the line above it.

Anyway, to expand on Brian’s answer:

$ cat f
FC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.13.118.99.112.45.48.47.49.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.48.47.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.48.47.48.47.49.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.49.47.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.49.47.48.47.49.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.48.46.51.50.55.54.56
= INTEGER: 1
RFC1155-SMI::enterprises.2636.3.40.1.4.1.2.1.3.1.15.118.99.112.45.50.47.48.47.49.46.51.50.55.54.56
= INTEGER: 1

$ ruby -ane ‘puts $F[2] if /^= INTEGER: /’ < f
1
1
1
1
1
1
1
1

What happens when the pastebin goes down?

-Josh

People should just learn to use pastebins.

– Matma R.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs