Forum: Ruby putting values in one lines

Posted by Ferdous ara (ferdous)
on 2012-11-06 23:21
Hi,
Dont know how to explain this.

example , i am doing this

output=`cat #{file} | grep  "up    down" | grep "aenet" | awk '{print
$6}'`
puts output


So i get this
ae40.0
ae30.0

But i want to output like ae40.0 ae30.0

how can i achieve this ??

Thanks for your help.
Posted by Robert Klemme (robert_k78)
on 2012-11-06 23:40
(Received via mailing list)
On Tue, Nov 6, 2012 at 11:21 PM, Ferdous ara <lists@ruby-forum.com> 
wrote:

> So i get this
> ae40.0
> ae30.0
>
> But i want to output like ae40.0 ae30.0
>
> how can i achieve this ??
>

First I'd start by not using shell tools for this in a Ruby script (and
btw. the cat is useless).  Then we'd need to know the input to come up 
with
suggestions.

Cheers

robert
Posted by Ferdous ara (ferdous)
on 2012-11-06 23:53
.  Then we'd need to know the input to come up

Hi , the output is coming from a router

and the output has too much rough data , thats why i was using BAck tick 
..
example

spawn

--- JUNOS xxxxx  built 2012-03-17 18:55:36 UTC
{master:10

--- JUNOS xxxxxbuilt 2012-03-17 18:55:36 UTC
{master:10}

syntax error, expecting <command>.
 configure systemconsole
                                      ^
syntax error, expecting <command>.

{master:10
set output

xe-0/0/2.0              up    up   aenet    --> ae1.0
xe-0/0/3                up    up
xe-0/0/3.0              up    up   aenet    --> ae1.0

xe-10/0/6.0             up    down aenet    --> ae40.0
xe-24/0/3.0             up    down aenet    --> ae30.0

then......................

what i am after is like
xe-10/0/6.0             up    down aenet    --> ae40.0


Thanks  for your help
Posted by 7stud -- (7stud)
on 2012-11-07 00:18
require 'stringio'

f = StringIO.new(<<ENDOFSTRING)
xe-0/0/2.0      up    up   aenet    --> ae1.0
xe-0/0/3        up    up
xe-0/0/3.0      up    up   aenet    --> ae1.0

xe-10/0/6.0     up    down aenet    --> ae40.0
xe-24/0/3.0     up    down aenet    --> ae30.0
ENDOFSTRING

target_column = 6

results = f.grep(/up \s* down \s* aenet/xms)
results.each {|line| puts line.split[target_column - 1]}


--output:--
ae40.0
ae30.0
Posted by Ferdous ara (ferdous)
on 2012-11-07 00:31
> --output:--
> ae40.0
> ae30.0


HI thanks

only problem is, i want output like

ae40.0 ae30.0

in one line ..

Thanks
Posted by 7stud -- (7stud)
on 2012-11-07 00:38
require 'stringio'

f = StringIO.new(<<ENDOFSTRING)
xe-0/0/2.0      up    up   aenet    --> ae1.0
xe-0/0/3        up    up
xe-0/0/3.0      up    up   aenet    --> ae1.0

xe-10/0/6.0     up    down aenet    --> ae40.0
xe-24/0/3.0     up    down aenet    --> ae30.0
ENDOFSTRING


results = ""

f.each do |line|
  md = line.match(/
        up
        \s*
        down
        \s*
        aenet
        .*?
        -->
        \s*
        (.*)
        \n
        \z
  /xms)

  if md
    results << $1 << " "
  end
end

p results.rstrip


--output:--
"ae40.0 ae30.0"
Posted by Ryan Davis (Guest)
on 2012-11-07 00:50
(Received via mailing list)
On Nov 6, 2012, at 15:39 , 7stud -- <lists@ruby-forum.com> wrote:

>  if md
>    results << $1 << " "
>  end
> end
>
> p results.rstrip

results = []

...
results << $1 if md
...

puts results.join " "
Posted by tamouse mailing lists (Guest)
on 2012-11-07 02:42
(Received via mailing list)
If you're hoping to do a pure ruby implementation of this, heed the 
others.

If you just need the values, here's the awk:

awk 'BEGIN { ORS="" } /up.*down.*aenet/ { print $6 }' file...

or,

awk -v ORS='' '/up.*down.*aenet/ { print $6 }' file...

(you don't need the cat or either grep)
Posted by tamouse mailing lists (Guest)
on 2012-11-07 03:16
(Received via mailing list)
Ok, well, pure ruby:

  lines = IO.readlines(file)
  lines.each do |l|
    if l.match(/up\s+down\s+aenet/)
      print l.split[5], " "
    end
  end
  print "\n"


On Tue, Nov 6, 2012 at 7:37 PM, tamouse mailing lists
Posted by tamouse mailing lists (Guest)
on 2012-11-07 05:11
(Received via mailing list)
Ok, more compact:

output = []
IO.readlines("data").map{|l| output << l.split[5] if
l.match(/up\s+down\s+aenet/) }
puts output.join " "


On Tue, Nov 6, 2012 at 8:15 PM, tamouse mailing lists
Posted by tamouse mailing lists (Guest)
on 2012-11-07 05:48
(Received via mailing list)
Okay: one line:

puts IO.readlines("data").grep(/up\s+down\s+aenet/).map{|l| 
l.split[5]}.join " "


On Tue, Nov 6, 2012 at 10:11 PM, tamouse mailing lists
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.