From: timr Date: 2009-12-06T11:55:22+09:00 Subject: Re: awk to ruby 1.9 You can certainly recreate the awk functionality. But for the lazy programmer who likes the tool that already exists try something like: awk_test.txt this is some text 4 ___________________________ >>search_term = "some" >>data = 2 >>file = "awk_test.txt" >> open("|awk -F '#{search_term}' '{print $#{data}}' #{file}"){|f| @results = f.gets} => " \ttext\t4\n" >> puts @results text 4 You can also use the IO class to create pipe to a unix command like awk or agrep in this example(code I have in my .irbrc file): #you need to have TEV agrep installed to use this code def agrep_with_string(regex, string) temp_file = 'temp_file_for_agrep' File.open(temp_file, 'w'){|f| f.puts string} str = "agrep --color -e '#{regex.source}' #{temp_file}" IO.popen(str){|io| @hits = io.readlines} File.delete(temp_file) @hits end On Dec 4, 12:09 pm, Panagiotis Atmatzidis wrote: > Hello, > > I'm a total newbe. I'm trying to figure out how to grab a specific Xth character from a .log and put it into an array. > > So far, I've got to this code snippet: > > ------------ > # encoding: utf-8 > class ReadIPs >   def initialize >     @read_ips = [] >   end > >   def get_ips(filename) >     ips = [] # make a list of ip addressses >     File.foreach(filename) do |line| >        puts "#{line}" if line =~ /Ban/ >        end >   end > end > > ipadds = ReadIPs.new > a = ipadds.get_ips("fail2ban.log") > -------------- > > The output though parses lines the following: > > 2009-11-19 00:31:29,928 fail2ban.actions: WARNING [ssh-ipfw] Ban 203.169.139.171 > > Now, I'd like to isolate the IP and put it into an Array. > > I would like to use the output from the cli: "$ grep Ban fail2ban.log|awk -F "Ban" '{print $2}'" > >  60.12.200.xx >  193.193.221.xx >  85.72.xx.xx >  124.207.xx.xx > > regards > > Panagiotis (atmosx) Atmatzidis > > email:  a...@convalesco.org > URL:    http://www.convalesco.org > GnuPG ID: 0xFC4E8BB4 > gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xFC4E8BB4 > -- > The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."