Search string in a file

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Re: Search string in a file

    On Wed, 15 Oct 2003, Panther wrote:

    # I must search string in a file ??
    # How I must open file in read and search string ??
    # What is syntax ??
    # Thamk you
    #

    ruby -ne 'print if /mystring/' filename.txt


    Chad Fowler Guest

  2. Similar Questions and Discussions

    1. search a string for value
      hi, i have a field that has descriptions for a product.. i want to do a search on this field.. but i'm getting bad results because some users...
    2. String Search with SQL
      I was trying to write an SQL statment that will do a string search in an Access 2000 database. But I can't seem to figure out how to do it so that...
    3. how to optimize a string search
      I know this is more of an algorithm question but please bear with me. In my program I am checking wether a emailid exists in a list I have in...
    4. String question: Returning portion of string with words surrounding highlighted search term?
      I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string,...
    5. string search
      Is there a way to search a string within a string. For example, I want to search for "world" in "Hello world" knowing that "world" can be anywhere...
  3. #2

    Default Re: Search string in a file


    "Chad Fowler" <chad@chadfowler.com> schrieb im Newsbeitrag
    news:Pine.LNX.4.44.0310150841280.26441-100000@ns1.chadfowler.com...
    > On Wed, 15 Oct 2003, Panther wrote:
    >
    > # I must search string in a file ??
    > # How I must open file in read and search string ??
    > # What is syntax ??
    > # Thamk you
    > #
    >
    > ruby -ne 'print if /mystring/' filename.txt
    I guess the OP is searching for a ruby solution, that can be embedded in a
    script. Otherwise this is likely to be simpler and more efficient:

    fgrep 'mystring' filename.txt

    Now it depends on whether to just verify the string is there:

    def check_file( file, string )
    File.open( file ) do |io|
    io.each {|line| line.chomp! ; return true if line.include? string}
    end

    false
    end

    or whether to get the first matching line:

    def check_file( file, string )
    File.open( file ) do |io|
    io.each {|line| line.chomp! ; return line if line.include? string}
    end

    nil
    end

    or whether to get all matching lines:

    def check_file( file, string )
    lines=[]

    File.open( file ) do |io|
    io.each {|line| line.chomp! ; lines << line if line.include? string}
    end

    lines
    end

    or whether the condition is a regexp:

    def check_file( file, rx )
    File.open( file ) do |io|
    io.each {|line| line.chomp! ; return true if rx =~ line}
    end

    false
    end

    etc.

    Cheers

    robert

    Robert Klemme Guest

  4. #3

    Default Re: Search string in a file

    In article <bmjh13$nopuc$1@ID-52924.news.uni-berlin.de>, Robert Klemme wrote:
    >> ruby -ne 'print if /mystring/' filename.txt
    >
    > I guess the OP is searching for a ruby solution, that can be embedded in a
    > script. Otherwise this is likely to be simpler and more efficient:
    >
    > fgrep 'mystring' filename.txt
    >
    > [...snip...]
    How about

    file.grep(/mystring/) { |s| puts s }

    ?
    Jason Williams Guest

  5. #4

    Default Re: Search string in a file


    "Jason Williams" <jason@jasonandali.org.uk> schrieb im Newsbeitrag
    news:slrnboqige.dt.jason@kotu.jasonandalishouse.or g.uk...
    > In article <bmjh13$nopuc$1@ID-52924.news.uni-berlin.de>, Robert Klemme
    wrote:
    > >> ruby -ne 'print if /mystring/' filename.txt
    > >
    > > I guess the OP is searching for a ruby solution, that can be embedded
    in a
    > > script. Otherwise this is likely to be simpler and more efficient:
    > >
    > > fgrep 'mystring' filename.txt
    > >
    > > [...snip...]
    >
    > How about
    >
    > file.grep(/mystring/) { |s| puts s }
    I guess you mean:

    File.open( file ) {|io| io.grep(/mystring/) { |s| puts s }}
    or
    File.open( file ) {|io| io.grep(/mystring/)}

    That doesn't get rid of line terminations. One can do

    File.open( file ) {|io| io.grep(/mystring/).each{|line|line.chomp!}}

    Yes, that's nicer if one does not want the simple check / first line. In
    those cases it's more efficient for large files to short circuit at the
    first match.

    Cheers

    robert

    Robert Klemme Guest

  6. #5

    Default Re: Search string in a file

    Hi!

    * Panther; 2003-10-15, 12:00 UTC:
    > I must search string in a file ??
    > How I must open file in read and search string ??
    > What is syntax ??
    Two are possible. The following reads file 'groups' as a whole and
    then prints out all lines that match /anime/ Regexp:

    File.readlines("groups").each{ |line|
    print line if line =~ /anime/
    }

    You can alternatively open "groups" and repeat the following until
    EOF has been reached: Read in one line and print it if it matches
    /anime/ Regexp.

    File.open("groups") { |file|
    while file.gets
    print $_ if /anime/
    end
    }

    HTH

    Please take notice of signature! / Bitte Signature beachten!

    Josef 'Jupp' Schugt
    --
    db Wenn sie mir ohne meine Einwilligung geschickt wurde, db
    dpqb wird eine E-Mail > 100 kB ungelesen entsorgt dpqb
    dp::qb If you send me an e-mail > 100 kB without my dp::qb
    dp::::qb consent it will be silently discarded dp::::qb

    Josef 'Jupp' SCHUGT Guest

  7. #6

    Default Re: Search string in a file (tangent)

    On Thu, 16 Oct 2003 06:54:07 +0900,
    Josef 'Jupp' SCHUGT <jupp@gmx.de> wrote:
    > File.readlines("groups").each{ |line|
    ^^^^^^^^^^^^^^^^^^^^^^^^
    It just struck me that this is horrible. It would be far nicer as

    File("groups").readlines.each{ |line|

    since then the filename is associated with the File part, not detached
    and attached to some random method. Same goes for opening a file.

    File("groups").open { |file|

    Perhaps I'm crazy, but that reads far more naturally to me ("create
    a file object on this file, then do something to it".)

    I might even submit that as an RCR since it's so lovely.
    Rob Partington Guest

  8. #7

    Default Re: Search string in a file (tangent)


    "Rob Partington" <rjp@frottage.org> schrieb im Newsbeitrag
    news:slrnbp9sac.hid.rjp@ipy.frottage.org...
    > On Thu, 16 Oct 2003 06:54:07 +0900,
    > Josef 'Jupp' SCHUGT <jupp@gmx.de> wrote:
    > > File.readlines("groups").each{ |line|
    > ^^^^^^^^^^^^^^^^^^^^^^^^
    > It just struck me that this is horrible. It would be far nicer as
    >
    > File("groups").readlines.each{ |line|
    >
    > since then the filename is associated with the File part, not detached
    > and attached to some random method. Same goes for opening a file.
    >
    > File("groups").open { |file|
    Hmmm, I don't like that. File is a class and open is a class method. So
    File.open(name) is perfectly ok for me.
    > Perhaps I'm crazy, but that reads far more naturally to me ("create
    > a file object on this file, then do something to it".)
    >
    > I might even submit that as an RCR since it's so lovely.
    You can do:

    class FileProxy
    def initialize(fileName)
    @fileName = fileName
    end

    def method_missing(sym, *args)
    File.open(@fileName) do |io|
    io.send( sym, *args )
    end
    end

    def open(mode="r")
    File.open(@fileName, mode)
    end

    end

    module Kernel
    def File(name)
    FileProxy.new(name)
    end
    end


    now you can do

    File("groups").readlines.each{ |line| ... }
    File("groups").open.readlines.each{|line| ... }

    But personally I don't like this construction since it requires a
    temporary instance that does nothing else but stores the file name for
    later use. And you don't really gain much readbility...

    Another thing I'd like to mention is that for large files
    File.readlines().each might be awfully imperformant since the whole file
    has to be sucked into mem. In those cases I prefer the block form anyway:

    File.open("groups"){|io| io.each {|line| ...} }

    Regards

    robert

    Robert Klemme Guest

  9. #8

    Default Re: Search string in a file (tangent)

    il 21 Oct 2003 08:41:48 GMT, Rob Partington <rjp@frottage.org> ha
    scritto::
    >On Thu, 16 Oct 2003 06:54:07 +0900,
    >Josef 'Jupp' SCHUGT <jupp@gmx.de> wrote:
    >> File.readlines("groups").each{ |line|
    > ^^^^^^^^^^^^^^^^^^^^^^^^
    >It just struck me that this is horrible. It would be far nicer as
    >
    > File("groups").readlines.each{ |line|
    >
    >since then the filename is associated with the File part, not detached
    >and attached to some random method. Same goes for opening a file.
    >
    > File("groups").open { |file|
    >
    >Perhaps I'm crazy, but that reads far more naturally to me ("create
    >a file object on this file, then do something to it".)
    I think this is really subjective. But I would not understand the 2nd
    choice easily.


    gabriele renzi Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139