473,324 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

key search in files

i am trying to find a set of keys within specific files under a specific
directory. i read the keys from a file and iterate through them opening
and looking all the files under the specified directory. However only
the last key seems to be found in the files..

Expand|Select|Wrap|Line Numbers
  1. srcFiles = Dir.glob(File.join("**", "*.txt"))
  2. keys = File.readlines("sp.txt")
  3.  
  4. keys.each{ |key|
  5.   srcFiles.each{|src|
  6.     linenumber = 0
  7.     File.readlines(src).each{ |line|
  8.       linenumber += 1
  9.       if line.include? key then
  10.       puts "found #{key}"
  11.     }
  12.   }
  13. }
May 4 '10 #1

✓ answered by improvcornartist

It looks like when you read in the keys, each key has a newline character at the end. So when you look in other files for that key, it doesn't find it. If the final key is not followed by a new line, then it would probably be more likely to find that one. I would try to chomp the key before searching for it in a line.

1 1918
improvcornartist
303 Expert 100+
It looks like when you read in the keys, each key has a newline character at the end. So when you look in other files for that key, it doesn't find it. If the final key is not followed by a new line, then it would probably be more likely to find that one. I would try to chomp the key before searching for it in a line.
May 4 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: RiceGuy | last post by:
Hi! I'm looking for ideas on what would the best approach to design a search system for a RSS feeds. I will have some 50 RSS feeds (all RSS 2.0 compliant) stored locally on the web server. Now I'm...
2
by: tmb | last post by:
I'm trying to use the Microsoft Search to search for a text string in a folder full of a bunch of ASP files. Seems like the normal "Search for text in files" program in XP Pro won't search...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
3
by: Ya Ya | last post by:
I have a folder with a lot of PDF and CHM files. I would like to develope an ASP.net application that enables the user to search inside the content of those files. How do I search inside those...
1
by: Rachelle | last post by:
I'm having some trouble with the search field that i am setting up using PHP and HTML, I am connecting to a database to return proper information pertaining to something being searched. I...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
5
by: Mark | last post by:
Hi I have an application (in vb.NET 2005) which holds data in SQL Server and some of the SQL records are simply paths to related files. I would like to be able to do a text search on both the...
9
by: Lloyd Sheen | last post by:
For all those who don't think that a recursive search of files in folders is a good thing in the Microsoft.VisualBasic.FileIO.FileSystem namespace listen to this. I am reorg my mp3 collection. ...
3
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.