File compare and remove lines

(Matt Coffman) #1

Here is my issue:

I have two files - call them big.txt and small.txt. My goal is to have
a list file and remove any lines in the big file that exist.

Big.txt is a tab del file with the following info

11 Fred Flinestone Bedrock,Il
12 Wilma Flinestone Bedroc, Il

small.txt is a tab del file - following information
12
23
32
23

I would like to scan big.txt against small.txt to get a final file
good.txt - with the following

11 Fred Flinestone Bedrock,Il

regards,

matt

···

--
Posted via http://www.ruby-forum.com/.

(James Edward Gray II) #2

Which part are you hung up on? We'll help get you unstuck.

James Edward Gray II

···

On Aug 22, 2006, at 4:27 PM, Matt Coffman wrote:

I would like to scan big.txt against small.txt to get a final file
good.txt - with the following

11 Fred Flinestone Bedrock,Il

(Max Muermann) #3

bad = []
File.read("small.txt").each { |line| bad << line.to_i }
good = File.new("good.txt","w");
File.read("big.txt").each do |line|
      id = line.split('\t')[0].to_i
      good << line unless bad.delete(id)
end
good.close

I'm sure there's a better way, but this took only two minutes to write.
Max

···

On 8/23/06, Matt Coffman <matt@binarybrain.net> wrote:

Here is my issue:

I have two files - call them big.txt and small.txt. My goal is to have
a list file and remove any lines in the big file that exist.

Big.txt is a tab del file with the following info

11 Fred Flinestone Bedrock,Il
12 Wilma Flinestone Bedroc, Il

small.txt is a tab del file - following information
12
23
32
23

I would like to scan big.txt against small.txt to get a final file
good.txt - with the following

11 Fred Flinestone Bedrock,Il

regards,

matt

--
Posted via http://www.ruby-forum.com/.

(Matt Coffman) #4

James Gray wrote:

···

On Aug 22, 2006, at 4:27 PM, Matt Coffman wrote:

I would like to scan big.txt against small.txt to get a final file
good.txt - with the following

11 Fred Flinestone Bedrock,Il

Which part are you hung up on? We'll help get you unstuck.

James Edward Gray II

Actually the entire thing - I am currently using VEDIT to work through
this. I am a newbie to Ruby and wanted to give it a shot. Thanks in
advance

Matt

--
Posted via http://www.ruby-forum.com/.

(Jonathan Hankins) #5

How about this (heh, just discovered File#readlines!):

···

--
#!/usr/bin/ruby -w

skip = Hash.new

File.readlines(ARGV.shift).each do |line|
  skip[line.chomp.strip] = 1
end

File.readlines(ARGV.shift).each do |line|
  print line unless skip[line.match(/^\s*(\d+)/)[1]]
end
--
+------------------+-----------------------------------------------------+

Jonathan Hankins | jonathan-hankins@mindspring.com |

+------------------+-----------------------------------------------------+

(James Edward Gray II) #6

So you are asking us to write it for you? How is that you deciding to give Ruby a shot? :wink:

James Edward Gray II

···

On Aug 22, 2006, at 4:45 PM, Matt Coffman wrote:

James Gray wrote:

On Aug 22, 2006, at 4:27 PM, Matt Coffman wrote:

I would like to scan big.txt against small.txt to get a final file
good.txt - with the following

11 Fred Flinestone Bedrock,Il

Which part are you hung up on? We'll help get you unstuck.

James Edward Gray II

Actually the entire thing - I am currently using VEDIT to work through
this. I am a newbie to Ruby and wanted to give it a shot. Thanks in
advance

(Paul Lutus) #7

Matt Coffman wrote:

James Gray wrote:

I would like to scan big.txt against small.txt to get a final file
good.txt - with the following

11 Fred Flinestone Bedrock,Il

Which part are you hung up on? We'll help get you unstuck.

James Edward Gray II

Actually the entire thing - I am currently using VEDIT to work through
this. I am a newbie to Ruby and wanted to give it a shot.

Give what a shot? When you go on a bear hunt, do you tell someone else to go
get the bear for you, while you wait in the lounge?

Your first post specified this goal:

I have two files - call them big.txt and small.txt. My goal is to have
a list file and remove any lines in the big file that exist.

Is that your goal or a goal for your alter ego, who is now out looking for a
suitable bear?

Thanks in advance

You are welcome in advance.

People learn to write computer programs primarily by writing computer
programs, not by asking other people to write them.

You need to realize that those with some Usenet experience, when reading a
message like yours, instinctively ask themselves, "Is this someone trying
to avoid doing his own homework?"

In order to become as rich as, say, Bill Gates, you must first learn how to
write computer programs. Remember that Bill Gates once actually wrote
computer programs, by himself, and he was rather good at it. He didn't ask
someone else to do the programming (at least, not at first).

My goal is to have
a list file and remove any lines in the big file that exist.

... and a good first step would be to specify the problem with perfect
clarity.

Please understand that people on this newsgroup would leap at the chance to
help someone who is sincerely trying to learn Ruby. All we need is a sign
that you are sincere.

···

On Aug 22, 2006, at 4:27 PM, Matt Coffman wrote:

--
Paul Lutus
http://www.arachnoid.com

(Matt Coffman) #8

James Gray wrote:

···

On Aug 22, 2006, at 4:45 PM, Matt Coffman wrote:

James Edward Gray II

Actually the entire thing - I am currently using VEDIT to work through
this. I am a newbie to Ruby and wanted to give it a shot. Thanks in
advance

So you are asking us to write it for you? How is that you deciding
to give Ruby a shot? :wink:

James Edward Gray II

Currently reading "Learn to Program" C.Pine

Not exactly - just looking for guidance - :slight_smile:

--
Posted via http://www.ruby-forum.com/.

(Matt Coffman) #9

Paul Lutus wrote:

Matt Coffman wrote:

James Edward Gray II

Actually the entire thing - I am currently using VEDIT to work through
this. I am a newbie to Ruby and wanted to give it a shot.

Give what a shot? When you go on a bear hunt, do you tell someone else
to go
get the bear for you, while you wait in the lounge?

Your first post specified this goal:

I have two files - call them big.txt and small.txt. My goal is to have
a list file and remove any lines in the big file that exist.

Is that your goal or a goal for your alter ego, who is now out looking
for a
suitable bear?

Thanks in advance

You are welcome in advance.

People learn to write computer programs primarily by writing computer
programs, not by asking other people to write them.

You need to realize that those with some Usenet experience, when reading
a
message like yours, instinctively ask themselves, "Is this someone
trying
to avoid doing his own homework?"

In order to become as rich as, say, Bill Gates, you must first learn how
to
write computer programs. Remember that Bill Gates once actually wrote
computer programs, by himself, and he was rather good at it. He didn't
ask
someone else to do the programming (at least, not at first).

My goal is to have
a list file and remove any lines in the big file that exist.

... and a good first step would be to specify the problem with perfect
clarity.

Please understand that people on this newsgroup would leap at the chance
to
help someone who is sincerely trying to learn Ruby. All we need is a
sign
that you are sincere.

As stated in my last post - I was looking for guidance. My sincere
apologies if it appears I am looking for someone to code for me. I am
new to this and I obviously lack certain skills - communicating my
problem being one of them. I appreciate your candid response but I
don't think the lecture is necessary.

···

--
Posted via http://www.ruby-forum.com/.

(James Edward Gray II) #10

Oh great. I can do that. Here's the steps I might try:

1. Read the small file into an Array
2. Write a loop that reads each line of the big file and prints it out
3. Modify the loop not to print things in the Array

See if that gets you going and do come back if you get stuck!

James Edward Gray II

···

On Aug 22, 2006, at 5:09 PM, Matt Coffman wrote:

James Gray wrote:

On Aug 22, 2006, at 4:45 PM, Matt Coffman wrote:

James Edward Gray II

Actually the entire thing - I am currently using VEDIT to work through
this. I am a newbie to Ruby and wanted to give it a shot. Thanks in
advance

So you are asking us to write it for you? How is that you deciding
to give Ruby a shot? :wink:

James Edward Gray II

Currently reading "Learn to Program" C.Pine

Not exactly - just looking for guidance - :slight_smile:

(Paul Lutus) #11

Matt Coffman wrote:

James Gray wrote:

James Edward Gray II

Actually the entire thing - I am currently using VEDIT to work through
this. I am a newbie to Ruby and wanted to give it a shot. Thanks in
advance

So you are asking us to write it for you?

Not exactly - just looking for guidance - :slight_smile:

Okay, here's some guidance.

Chances are Ruby will be able to perform the task you describe, once you
describe it with sufficient clarity, and once you acquire the necessary
programming skill, step by step, patiently and persistently.

We'll know you are on track to solve the problem when you start a post with
"here's my code so far ... and here's what happened when I ran it ..."

···

On Aug 22, 2006, at 4:45 PM, Matt Coffman wrote:

--
Paul Lutus
http://www.arachnoid.com

(Paul Lutus) #12

Matt Coffman wrote:

As stated in my last post - I was looking for guidance. My sincere
apologies if it appears I am looking for someone to code for me. I am
new to this and I obviously lack certain skills - communicating my
problem being one of them. I appreciate your candid response but I
don't think the lecture is necessary.

Oh, but it was. Now you know that you need to show your sincerity by posting
your code and your questions about your code. You didn't know that before.
You are way ahead, and the next steps are yours.

Steps like these:

1. Describe the problem to be solved with perfect clarity, such that no
typical reader could possibly have any doubt about the problem to be
solved. One way to do this is to say "Here is sample content for the first
file ... and the second file .. and here is what the program's output
should look like ..."

2. Show us that you intend to get personally involved by posting your code.
If you cannot post code, post pseudo-code, that is to say, "code" meant to
resemble steps toward solving the problem, even if the "code" isn't
actually executable.

As stated in my last post - I was looking for guidance.

This is the right guidance. You asked, and now you have it.

We await your reply.

···

--
Paul Lutus
http://www.arachnoid.com

(Matthew Johnson) #13

In order to become as rich as, say, Bill Gates, you must first learn how
to
write computer programs. Remember that Bill Gates once actually wrote
computer programs, by himself, and he was rather good at it. He didn't
ask
someone else to do the programming (at least, not at first).

Yes, but in order to become as rich as someone as, say, Steve Ballmer, you're free to have someone like Bill Gates do the programming for you (even at first). :slight_smile:

Matthew

(Matt Coffman) #14

Paul Lutus wrote:

Matt Coffman wrote:

So you are asking us to write it for you?

Not exactly - just looking for guidance - :slight_smile:

Okay, here's some guidance.

Chances are Ruby will be able to perform the task you describe, once you
describe it with sufficient clarity, and once you acquire the necessary
programming skill, step by step, patiently and persistently.

We'll know you are on track to solve the problem when you start a post
with
"here's my code so far ... and here's what happened when I ran it ..."

Again, thank you for the lecture. No need to respond! poumpous ass!

···

--
Posted via http://www.ruby-forum.com/.

(Matt Coffman) #15

Matthew Johnson wrote:

In order to become as rich as, say, Bill Gates, you must first
learn how
to
write computer programs. Remember that Bill Gates once actually wrote
computer programs, by himself, and he was rather good at it. He
didn't
ask
someone else to do the programming (at least, not at first).

Yes, but in order to become as rich as someone as, say, Steve
Ballmer, you're free to have someone like Bill Gates do the
programming for you (even at first). :slight_smile:

Matthew

My sincere apologies to all.

Matt

···

--
Posted via http://www.ruby-forum.com/.

(Paul Lutus) #16

Matt Coffman wrote:

We'll know you are on track to solve the problem when you start a post
with
"here's my code so far ... and here's what happened when I ran it ..."

Again, thank you for the lecture. No need to respond! poumpous ass!

You've just gone over to the dark side, and you have yet to meaningfully
address the problem you originally posted about.

Has it occurred to you that you would refuse to accept your own attitude and
language if you received it from another?

All I can say for sure at this stage is that you are very young, and you
have no behavioral reserves to speak of.

Try to return to the original topic. Thanks!

···

--
Paul Lutus
http://www.arachnoid.com

(Carlos) #17

Matt Coffman wrote:

My sincere apologies to all.

Matt

Where to can we redirect newbies asking for help, before people start to spit rude answers to them and discourage them to learn Ruby (or to let us help them to learn Ruby)? Is there any ruby-newby mailing list/newsgroup?

···

--

(Chad Perrin) #18

Frankly, I wonder if maybe you both couldn't use some advice on mailing
list etiquette.

···

On Wed, Aug 23, 2006 at 07:45:10AM +0900, Paul Lutus wrote:

Matt Coffman wrote:

>> We'll know you are on track to solve the problem when you start a post
>> with
>> "here's my code so far ... and here's what happened when I ran it ..."
>
> Again, thank you for the lecture. No need to respond! poumpous ass!

You've just gone over to the dark side, and you have yet to meaningfully
address the problem you originally posted about.

Has it occurred to you that you would refuse to accept your own attitude and
language if you received it from another?

All I can say for sure at this stage is that you are very young, and you
have no behavioral reserves to speak of.

Try to return to the original topic. Thanks!

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"There comes a time in the history of any project when it becomes necessary
to shoot the engineers and begin production." - MacUser, November 1990

(Paul Lutus) #19

Carlos wrote:

Matt Coffman wrote:

My sincere apologies to all.

Matt

Where to can we redirect newbies asking for help, before people start to
spit rude answers to them and discourage them to learn Ruby (or to let
us help them to learn Ruby)? Is there any ruby-newby mailing
list/newsgroup?

AFAIK there is only this one Ruby NG, and I agree it might be better if
there were two or more, for specific needs. A symptom indicating this need
(apart from what you point out) is the rather high volume of posts to this
one NG, on diverse subjects related to Ruby.

But I must say the OP would have gotten the same reception from a Ruby NG
geared to newbies ... he would eventually be asked to post some code. So
the outcome would have been the same.

···

--
Paul Lutus
http://www.arachnoid.com

(Steve Litt) #20

I know of no Ruby-Newbie mailing lists or newsgroups, but there are many Ruby
tutorials on the web. I've created one here:

http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm

As far as rude answers, a newbie question is no excuse for rudeness. It's so
much easier simply to not answer the newbie question, or to have a stock
signature block that, in the nicest way possible, says RTFM and tells the
newbie where the fantastic manual resides.

SteveT

Steve Litt
Author:
   * Universal Troubleshooting Process courseware
   * Troubleshooting Techniques of the Successful Technologist
   * Manager's Guide to Technical Troubleshooting
   * Twenty Eight Tales of Troubleshooting
   * Rapid Learning: Secret Weapon of the Successful Technologist

http://www.troubleshooters.com/bookstore
http://www.troubleshooters.com/utp/tcourses.htm

···

On Wednesday 23 August 2006 09:18 am, Carlos wrote:

Matt Coffman wrote:
> My sincere apologies to all.
>
> Matt

Where to can we redirect newbies asking for help, before people start to
spit rude answers to them and discourage them to learn Ruby (or to let
us help them to learn Ruby)? Is there any ruby-newby mailing
list/newsgroup?