From: "Jesús Gabriel y Galán" Date: 2009-12-24T02:23:48+09:00 Subject: Re: awk print $4 in ruby On Wed, Dec 23, 2009 at 6:06 PM, Derek Smith wrote: > Derek Smith wrote: >>>>      df -k | ruby -lane 'print $F[3]' >>> >>> yes and no.  I prefer it in script form no CLI form. >>> >>> dfstr = Array.new >>> dfstr << %x(df -m) >>> dfstr.each do |line| >>>     ????? >>> end >> >> Tried this too: >> >> $; = 'Avail' >> dfstr = %x(df -m) >> puts dfstr[$;] > > I got it using, but ideally would not like to use a file on the FS. > Any comments welcome! :) > > Merry Xmas! > Thank you! > > > DFSTR = "/tmp/dfstr.out" > %x(df -m > "#{DFSTR}") > file = File.open("#{DFSTR}", "r") > file.each do |ln| >    ln.chomp >    fsaray = [] >    fsaray = ln.split >    puts fsaray[3] > end You don't need a file, cause df will return a string with each line separated by "\n", which the "each" method in string defaults to as a separator: irb(main):001:0> s = %x{df -k} irb(main):002:0> s.each {|line| p line} "Filesystem 1K-blocks Used Available Use% Mounted on\n" "/dev/sda1 147549816 17405644 122649048 13% /\n" "tmpfs 1815116 0 1815116 0% /lib/init/rw\n" "varrun 1815116 132 1814984 1% /var/run\n" "varlock 1815116 0 1815116 0% /var/lock\n" "udev 1815116 2864 1812252 1% /dev\n" "tmpfs 1815116 560 1814556 1% /dev/shm\n" "lrm 1815116 2204 1812912 1% /lib/modules/2.6.27-14-generic/volatile\n" So, now, for each line, you want to split as you did and take the 4th element: irb(main):003:0> s.map {|line| line.split[3]} => ["Available", "122649048", "1815116", "1814984", "1815116", "1812252", "1814556", "1812912"] If you want to output this array, one element in each line, you can use puts on the full array: irb(main):004:0> puts s.map {|line| line.split[3]} Available 122649048 1815116 1814984 1815116 1812252 1814556 1812912 Hope this helps, Jesus.