[#407177] Downloading a CSV using URI - mechanize — Rochit Sen <lists@...>

Hi All,

13 messages 2013/05/06

[#407178] %tempdir% location and folder clean up? — "Abinash Y." <lists@...>

Hi Guys,

12 messages 2013/05/06

[#407187] Gem Help - Newbie Question — Michael Tepfer <lists@...>

Hi,

17 messages 2013/05/07

[#407234] Problem with FileUtils move command. — Peter Bailey <lists@...>

Hello,

25 messages 2013/05/08
[#407235] Re: Problem with FileUtils move command. — Joel Pearson <lists@...> 2013/05/08

"no implicit conversion of nil into String" means you're probably

[#407236] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Joel Pearson wrote in post #1108209:

[#407238] Re: Problem with FileUtils move command. — Chris Hulan <chris.hulan@...> 2013/05/08

how is the script invoked?

[#407239] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Chris Hulan wrote in post #1108213:

[#407242] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

can you post some of the script?

[#407246] Re: Problem with FileUtils move command. — Love U Ruby <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108224:

[#407250] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

What do you get if you puts "ARGV == #{ARGV[0]}" ?

[#407251] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108238:

[#407252] Re: Problem with FileUtils move command. — Daniel Stephens <danny.a.stephens@...> 2013/05/08

So it's like the other guys mentioned, there appears to be no file(or

[#407255] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Daniel Stephens wrote in post #1108240:

[#407257] Re: Problem with FileUtils move command. — Hassan Schroeder <hassan.schroeder@...> 2013/05/08

On Wed, May 8, 2013 at 6:54 AM, Peter Bailey <lists@ruby-forum.com> wrote:

[#407261] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Hassan Schroeder wrote in post #1108253:

[#407263] Re: Problem with FileUtils move command. — Hassan Schroeder <hassan.schroeder@...> 2013/05/08

On Wed, May 8, 2013 at 8:22 AM, Peter Bailey <lists@ruby-forum.com> wrote:

[#407265] Re: Problem with FileUtils move command. — Peter Bailey <lists@...> 2013/05/08

Hassan Schroeder wrote in post #1108265:

[#407258] Connect to a switch with SSH — "Rudá G." <lists@...>

I need connect to a Cisco Switch with SSH and Ruby. The main problem is

17 messages 2013/05/08

[#407298] Trucking All Http Request from a web page — Ja Tse <lists@...>

Hi,

10 messages 2013/05/09

[#407383] Enum#each issue — Love U Ruby <lists@...>

I am not able to understand why I am getting the error for the method

26 messages 2013/05/13

[#407464] Is it 'safe' to upgrade to ruby 2.0 by now? — Panagiotis Atmatzidis <atma@...>

Hello,

11 messages 2013/05/16

[#407471] Could you give me some Website for Learning Ruby — haxuan lac <lists@...>

I'm learning Ruby and I search some Website for learning Ruby

10 messages 2013/05/16

[#407517] Ruby Programming Practice — "buzz k." <lists@...>

Hey guys have been learning the basics of ruby and practicing some

30 messages 2013/05/19
[#407725] Re: Ruby Programming Practice — "buzz k." <lists@...> 2013/05/29

@Chad Perrin

[#407726] Re: Ruby Programming Practice — Stu <stu@...> 2013/05/29

On Wed, May 29, 2013 at 2:37 AM, buzz k. <lists@ruby-forum.com> wrote:

[#407740] Re: Ruby Programming Practice — Chad Perrin <code@...> 2013/05/29

On Wed, May 29, 2013 at 05:26:44PM +0900, Stu wrote:

[#407742] Re: Ruby Programming Practice — Brandon Weaver <keystonelemur@...> 2013/05/29

Skip FreeBSD and put armored plating on that bike by going OpenBSD.

[#407743] Re: Ruby Programming Practice — Chad Perrin <code@...> 2013/05/29

On Thu, May 30, 2013 at 02:54:51AM +0900, Brandon Weaver wrote:

[#407523] new, with an idea, and not sure what to learn next — Wendy Randquist <lists@...>

Hi. I've been going through the CodeAcademy courses for Ruby over the

13 messages 2013/05/19

[#407527] How to run shoes built via gem — Ruby Student <ruby.student@...>

This is perhaps a silly question.

14 messages 2013/05/19

[#407565] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...>

Hi,

15 messages 2013/05/21
[#407567] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — botp <botpena@...> 2013/05/21

On Tue, May 21, 2013 at 9:05 PM, Samuel Williams <

[#407585] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...> 2013/05/21

Hi botp, you can create any configuration of DNS you like. RubyDNS doesn't

[#407586] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — botp <botpena@...> 2013/05/22

On Wed, May 22, 2013 at 7:02 AM, Samuel Williams <

[#407587] Re: [ANN] RubyDNS - asynchronous DNS client and server for Ruby. — Samuel Williams <space.ship.traveller@...> 2013/05/22

Hi, you can use geo-ip for finding the location, geographically speaking.

[#407621] Regular expression to find a break in a pattern — Joel Pearson <lists@...>

I have a large file which lots of gibberish in and I'm trying to find

10 messages 2013/05/23

[#407639] On Green Shoes, do we have a broken arrow? — Ruby Student <ruby.student@...>

Team,

12 messages 2013/05/24

[#407715] AAARRRRGH! s="#$" => SyntaxError: compile error — Tadeusz Bochan <lists@...>

Hi.

13 messages 2013/05/28

[#407738] sending a gmail using ruby — David Munyaka <lists@...>

please help analyzing this code and please give me some recommendations.

26 messages 2013/05/29

[#407763] I can't unsubscribe, please help — Matthew Westerberg <matthew@...>

Hi,

11 messages 2013/05/29

[#407820] Eloquent Ruby Kaprekar's Number — Alphonse 23 <lists@...>

Which way is the eloquent ruby way to write this algorithm?

18 messages 2013/05/30

Re: output file from clipboard in unix

From: tamouse mailing lists <tamouse.lists@...>
Date: 2013-05-20 03:02:39 UTC
List: ruby-talk #407533
On Sun, May 19, 2013 at 12:01 PM, JD JD <lists@ruby-forum.com> wrote:
> I'm new to Ruby and pretty new to programming in general.  My limited
> prior experience is with JavaScript, so I'm not accustomed to handling
> file I/O or working with Unix/Terminal.  I'm working on a script that
> takes a large quantity of text copied from a web browser and does some
> reformatting so the result lines up neatly in a CSV and eventually a
> spreadsheet.  Basically, I'm using a few regex/gsub statements to remove
> unnecessary text from the beginning of the page and to add/remove tabs
> from certain places.  I've got two issues with this script, please see
> the attached files.
>
> 1. Right now, I can run the script in Terminal and direct the output to
> a new file and everything works (except for one of the regexes, see #2
> below), provided that I first save the text in a file that matches the
> file name used in the script.  If possible, I'd prefer to skip saving a
> file with the input text and allow the user to simply copy and paste it
> directly into Terminal.  In other words, you could run the script in
> Terminal, be prompted to paste your text in, and the script would make
> all
> the necessary changes to the text and output to a file.  I tried adding
> a prompt and gets statement to hold the text in a variable, but this
> just grabs a single line of text, terminates the Ruby script, and then
> produces a Terminal error for each subsequent line.  Is there any way I
> can accomplish this alternate behavior?

Let's look at this:

#read file and replace
fileObj = File.new("input.txt", "r")
while (line = fileObj.gets)
  substitute_line = line.gsub(clear_stuff, "")
  substitute_line1 = substitute_line.gsub(add_tabs, "\t\t\\1")
  substitute_line2 = substitute_line1.gsub(remove_tab, "\\1")
  print(substitute_line2)
end
fileObj.close

You can quite easily change that to use STDIN thusly:

#read file and replace
puts "Paste in your text below:"
while (line = gets)
  substitute_line = line.gsub(clear_stuff, "")
  substitute_line1 = substitute_line.gsub(add_tabs, "\t\t\\1")
  substitute_line2 = substitute_line1.gsub(remove_tab, "\\1")
  print(substitute_line2)
end

> 2. The regex I created to remove all unneeded content isn't working.  I
> checked this in Rubular and it worked as I expected, but it does not
> work when I run the script.  All the content that is needed in the
> output follows a unique set of table headers on the web page, so I
> should be able to find everything leading up to and including those
> headers and remove it.  The regex looks like this (with random terms
> added in place of the table headers):
>
> /.*hat\.goat\sthis thing\sthat thing\sstuff\scheese/m
>
> As it is, the headers do get replaced, but the content that precedes it
> does not.  I get the same result if I remove the wildcard at the
> beginning of the regex.  Any ideas?

Since you are working on the file one line at a time, it will not
match anything preceeding that (if I understand this correctly) so the
m modifier at the end really makes no sense there.

If you want to throw away everything up to and including the line that
matches that Regexp, you should probably consider the flip-flop
instead of a gsub at that point:

#read file and replace
puts "Paste in your input:"
while (line = gets)
  next if $. == 1 .. line =~ clear_stuff         # $. is a variable
that contains the
                    # number of the last line read
# from the file, in this case, STDIN
  print line.gsub(add_tabs, "\t\t\\1").gsub(remove_tab, "\\1")
# I've rewritten the two substitution
# lines as one chain. No need for
# for intermediate variables.
end

The flip-flop operator is rather interesting, if a bit arcane. What it
does is return false while the first part is false. When the first
part becomes true, it will return true up to the point the second
condition becomes true, then it will return false after that. (If you
use three dots: ... - the flip-flop becomes exclusive, not including
the last bit tested.)

So what the above is doing, is rolling through the input file, tossing
lines from the beginning, including the line that matches
`clear_stuff`. Then, as it continues to roll through, it fails the if
statement on the next, so drops through to the print statement and
performs the substitutions.

Since I can see already that gmail in it's stupidity will wrap my
code, I'm attaching it here as well as at
https://gist.github.com/anonymous/5610197

> Attachments:
> http://www.ruby-forum.com/attachment/8432/test_forum.rb
> http://www.ruby-forum.com/attachment/8433/input.txt
>
>
> --
> Posted via http://www.ruby-forum.com/.
>


Hope this helps...

Attachments (1)

filter.rb (325 Bytes, text/x-ruby)
#regexes for find/replace
add_tabs = /^(term1)/
remove_tab = /\t(\tterm2)/
clear_stuff = /.*hat\.goat\sthis thing\sthat thing\sstuff\scheese/m


#read file and replace
puts "Paste in your input:"
while (line = gets)
  next if $. == 1 .. line =~ clear_stuff
  print line.gsub(add_tabs, "\t\t\\1").gsub(remove_tab, "\\1")
end

In This Thread

Prev Next