Find string, get line number or position, then restart at +1

Hi All,

First off I want to say this list is very helpful and people are nice
and polite compared to many other email lists and forums I have been on
for years and years, notably the camel lists. Thank you!

Ok so I have a file where I am searching for the string “Jul 14.*” for
example using a regexp and this is working fine. However I have been
debating on how to move forward with what code, $. or tell with seek?

for each line in file
linenumber = $. if /^string/
end

for each line in file
linenumber = tell line if /^string/
end

Will either of the above pseudo-code work?

seek to linenumber and add 1
start re-reading at linenumber+1 and store data until EOF.

Please help and or provide sample code?

thank you

Derek S. wrote:

Huh?

7stud – wrote:

Derek S. wrote:

Huh?

Huh!
: )

Goal is to find pattern in file, note line number in same file, discard
all prior lines while storing all proceeding lines after pattern was
found?

I have the regexp working and the store working to a table, however
getting the syntax correct for the “start at linenumber +1 from pattern
was found” is evading me.

for each line in file
linenumber = $. if /^string/
end

for each line in file
linenumber = tell line if /^string/
end

Will either of the above pseudo-code work?

seek to linenumber and add 1
start re-reading at linenumber+1 and store data until EOF.

flag = false
results = []

DATA.each_line do |line|
results << line if flag
flag = true if line =~ /2/
end

p results

–output:–
[“line 3\n”, “line 4\n”]

END
line 1
line 2
line 3
line 4

Derek S. wrote:

Goal is to find pattern in file, note line number in same file, discard
all prior lines while storing all proceeding lines after pattern was
found?

f.each_with_index { |line,lineno| … } is a simple way to iterate over
the lines and have the line number (starting at 0) too.

I should point out that Ruby does inherit this horrible Perlism:

$ ruby -ne ‘print if /sshd/…/ntp/’ </etc/passwd
sshd:x:109:65534::/var/run/sshd:/usr/sbin/nologin
openwrt:x:1009:100::/home/openwrt:/bin/sh
ntp:x:115:115::/home/ntp:/bin/false
$

But for sanity reasons you should probably avoid it.

On Aug 26, 8:44 am, Derek S. [email protected] wrote:

for each line in file
start re-reading at linenumber+1 and store data until EOF.

Please help and or provide sample code?

thank you

Posted viahttp://www.ruby-forum.com/.

I’m not sure I’m understanding your question right, but it sounds like
you want all the data in a file after “Jul 14”? If so there’s no need
to traverse through and then seek. Try something like this:

regex = /^Jul 14/
found = false
data = Array.new()
file = File.open(“file”,“r”)
file.each{|line|
data << line if(found)
found = true if(line =~ regex)
}
puts data.join("\n")

This will put everything after the line starting with “Jul 14” into an
array delineated by lines. Then you can do whatever you want with it.
If you wanted to alter it so it grabs the data between two different
regexes just add another if(line =~ regex2) line and change found to
false. Hope this helps!

-Dylan

Dylan wrote:

regex = /^Jul 14/
found = false
data = Array.new()
file = File.open(“file”,“r”)
file.each{|line|
data << line if(found)
found = true if(line =~ regex)
}
puts data.join("\n")

That man is a genius. :slight_smile:

On Aug 26, 2009, at 12:43 PM, Derek S. wrote:

discard
all prior lines while storing all proceeding lines after pattern was
found?

OK, starting from just this, I’d say:

re = /^Jul 14/
lines = []
File.open(filename) do |file|
until file.eof?
break if file.gets =~ re
end

until file.eof?
lines << file.gets
end
end

… do something with lines

for each line in file
linenumber = tell line if /^string/
end

Will either of the above pseudo-code work?

seek to linenumber and add 1
start re-reading at linenumber+1 and store data until EOF.

I don’t know why you’d want to seek or re-read lines given your
parameters. Just the one pass is enough. Of course, if you can
operate on each line inside the second inner loop, you don’t even need
to accumulate them into the lines array.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]
+1 513-295-4739
Skype: rob.biedenharn

On Aug 26, 2009, at 08:44, Derek S. wrote:

Ok so I have a file where I am searching for the string “Jul 14.*” for
example using a regexp and this is working fine. However I have been
debating on how to move forward with what code, $. or tell with seek?

If it’s a small file, use StringScanner:

require ‘strscan’

text = File.read the_file
scanner = StringScanner.new text
scanner.scan /Jul 14/

ri StringScanner for details.

Eric H. wrote:

On Aug 26, 2009, at 08:44, Derek S. wrote:

Ok so I have a file where I am searching for the string “Jul 14.*” for
example using a regexp and this is working fine. However I have been
debating on how to move forward with what code, $. or tell with seek?

If it’s a small file, use StringScanner:

require ‘strscan’

text = File.read the_file
scanner = StringScanner.new text
scanner.scan /Jul 14/

ri StringScanner for details.

Thank you all for your replies!
I understand all of them!
Derek

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs