codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

In Ruby, how would I find all .csv files in a folder and print out path to the files that contain the word "meh"?


By : , Category : ruby

EDIT:

This method is significantly more direct and efficient:

d = Dir.new('.')
d.entries.select do |e| 
  /^.+.csv$/.match(e) && IO.readlines(e).grep(/meh/).length > 0
end

This should do it assuming you want to search the current directory

d = Dir.new('.')

# This will find all files whose path ends in .csv
csvs = d.entries.select {|e| /^.+.csv$/.match(e)}

# This will find all .csv files that contain one or more instance 
# of the pattern /meh/
mehs = csvs.select do |e| 
  f = File.open(e)
  [*f.each_line].grep(/meh/).length > 0
end
ReLated :

You can't print Word files directly from browser unless user has a word processor extension for browser like Zotero. An other option is to convert your doc file to PDF, HTML or image format. wvWare is a software you can do this with. see: http://wvware.sourceforge.net/

To create word files with PHPWord. see: http://phpword.codeplex.com/

With this you can save your doc(x) files and add the links to your php page. Or you can directly download file when opening a PHP page by creating custom headers:

<?php
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename="myfile.docx"');
header('Cache-Control: max-age=0');

// Code that generates DOC

// output the file to the browser
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
exit;
?>

See forum thread here: https://phpword.codeplex.com/discussions/225901

Use an if-else statment to check if w.length() == 5 or not.

Although you don't mention what problem you're trying to solve, since your code runs I'm going to assume the issue is that the output file is empty.

The reason for that is that you're not flushing or closing the file before your program exits. Just add a new_file.close() at the very end and that should get you going.

If you ever used cygwin, it includes xmllint which in turn has the --format option. This was my original approach. However xmllint did not format attributes the way I liked, so I have developed my own script. Since the word documents contain a lot of rsid noise, the script has an option to remove it.

I use the following worklflow:

  • get a word document, let say foo.docx
  • explode-docx -r foo.docx
  • edit foo.docx - make a small change
  • explode-docx -r foo.docx
  • kdiff3 foo foo.bak
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .