Differences

This shows you the differences between two versions of the page.

Link to this comparison view

awkinruby [2014/10/26 01:52] (current)
Line 1: Line 1:
 +===== Simple awk program (Extracts columns only) =====
 +<code ruby>
 +# column.rb
 +# extract a column from a multi column file
 +#
 +
 +if (ARGV.empty?​) then
 +        STDERR.puts "Need filename"​
 +        exit
 +end
 +
 +# note first argument must be filename, second must be column
 +fileName = ARGV[0]
 +columnStr = ARGV[1]
 +puts "​Filename = #{fileName} Column = #​{columnStr}"​
 +
 +if (FileTest.exists?​(fileName)) then
 +        lineNum = 1
 +        fin = File.open(fileName)
 +        fin.each_line { |line|
 +                elements = line.split(/​ /)
 +                if (elements.size < columnStr.to_i) then
 +                        STDERR.puts "#​{lineNum} does not have #​{columnStr} colum
 +ns"
 +                else
 +                        colNum = columnStr.to_i - 1
 +                        puts elements[colNum]
 +                end
 +                lineNum = lineNum.succ
 +        }
 +        fin.close
 +end
 +</​code>​
 +----
 +  * [[rubyinfo|Back to Ruby]]
  
awkinruby.txt ยท Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0