Look ahead condition

Hi,
may be the heading of the post is not right but i dont know how to
explain this.

basically.

suppose you have an array which has value like this

Check Fault Check :

Check Chassis fault :

Check email :
DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08 11:12:35

What i want to do is :

if i find a line which is example : "Check Fault Check : " ( which end
with : )
i want to know if there are any other lines under this line or not, if
there is not then i want to delete “Check Fault Check”

So basically
i want to delete bellow 2 lines as there is not any thing under those
line which does not END With “:”
Check Fault Check :
Check Chassis fault :

but I want to keep “Check email :” as there is a line under that…

Can this be done ??

please let me know

On Wed, Oct 9, 2013 at 4:01 PM, Fosiul A. [email protected]
wrote:

i want to know if there are any other lines under this line or not, if
Can this be done ??
It sounds as if you only want to delete empty lines. That’s fairly
easy. One way

File.foreach “your.file” do |line|
puts line unless /^\s*$/ =~ line
end

There are other ways with command line utilities, e.g.

$ egrep -v ‘^[[:space:]]*$’ your.file

Does that help?

Kind regards

robert

If I understand what you want (I’m not sure I do), try something like
this.
Someone else can probably give you a better regular expression. I’m not
sure this is a good one.
This reminds me that I need to devote more time to studying about
regular
expressions.

str = "Check Fault Check :

Check Chassis fault :

Check email :
DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08 11:12:35

test:

"

print str.gsub(/.+:\s*\n+(?=.+:\s*\n+|\n+$)/,"")

#########output#########
Check email :
DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08 11:12:35

Harry

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 09/10/2013 15:01, Fosiul A. wrote:

So basically i want to delete bellow 2 lines as there is not any
thing under those line which does not END With “:” Check Fault
Check : Check Chassis fault :

but I want to keep “Check email :” as there is a line under
that…

Dear Fosiul,

One way to do this would be to convert the content (I see you actually
say array, in which case, that reduces one step) to a some structure
and operate on that, then output that structure in the required
format. This enables the main structure to be converted into a hash,
filtered further, or similar.

Perhaps you might find the following example useful (this could be
simplified further):

content = " ... (text content) ... ".split("\n")

structure = content.slice_before(/:$/).select { |e|
    e.count { |e| !e.empty? } > 1 }

output = structure.flatten.join("\n")

An ordered explanation of the stages in structure is:

    • .slice_before(/:$/) : break the array by : at end of line
    • .select { … } : filter the array …
    • e.count { |e| !e.empty? } > 1 : … by including only subarrays
      which have more than merely the ‘key’ (e.g. ‘Check email :’)

Peace,
tiredpixel
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSVfgMAAoJEOFolTkanF7Va2QH/18os4tIkngo/ENokzIEtscO
qoxYzWSCSZp2f9bxKCG3HQvYtRn6ngVuLU9N7YEWUs5lgiohddVEYuH4dhCdCAY2
wN4cVMUkO/gRahKix0U5i86nHb+LiBfIai87HhqTkALzgs8CEKEvdr4RJzjJfUjd
sBd3Tij11V9is4B11K6HREHGOapZfu194rmBq35KHTqS/aymtPjbyhXt0hz6Oj+d
6vq9ZqzgLLlYMIRPxivsP7o1ywiZe023FxBZa0KKlmiwHk1uhex2ouIwDJc0CwGx
N3qHjAPMeK4FK/D2RWqjjYpagRXq2X2E+EqidCK+Rq7CWhGIV61kew1Bh/rIfn0=
=//fk
-----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

p.s. A slightly shorter way of writing

On 10/10/2013 01:42, tiredpixel wrote:

structure = content.slice_before(/:$/).select { |e| e.count { |e|
!e.empty? } > 1 }

would be to flip the logic and use

structure = content.slice_before(/:$/).reject { |e|
    e.count(&:empty?) > 1 }

allowing the count-block to be collapsed into the shortform.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.20 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSVfpBAAoJEOFolTkanF7V830IALZa02e/JHHkbt+6B7KfBCHG
8Z/+cmHlck1rPD8mAII2btUq1941W/6maMYrX/JQWLeUZRpUApTwD+5uKkwW+KpY
7v5QH1JkKNLUZBcIVH+PjMEs9uW6ERql+x9dXUx45Em/dWbtfLj3BZkpnCUIVBJL
zzDAr1O5CeGqcOf1lF6gcSNpshiXiCXcp86kzcFlrDskygEWAfGcLn3EQSLTFzO9
AciG1mQoIM4Ph6mvrxoK1kpyeoLr3wpBwUmOVba1Ns1oOmoCZi35dsIm2s2mP36M
0zlfAB+semOxCV1M4TRsEPTN5q2XKlHyE5Es5EUxhq6s9DjgXpL00xGuZDFFrDQ=
=Xloi
-----END PGP SIGNATURE-----

On Thu, Oct 10, 2013 at 1:25 AM, Harry K. [email protected]
wrote:

str = "Check Fault Check :

"

print str.gsub(/.+:\s*\n+(?=.+:\s*\n+|\n+$)/,“”)

#########output#########

Check email :
DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08 11:12:35

Ah, now I see I misunderstand the task. Here’s my regexp:

$ ./x.rb
Check email :
DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08 11:12:35

$ cat x.rb
#!/bin/ruby

content = DATA.read

content.gsub! %r{^[\w\s]+:\s*$\s*(?=^[\w\s]+:\s*$|\z)}m, ‘’

puts content

END
Check Fault Check :

Check Chassis fault :

Check email :
DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08 11:12:35

$

Not really sure whether that is better. In theory at least my version
should cope with different line endings; I did not test that though.

If the data is to be further processed then parsing into a proper data
structure would probably look different, e.g.

$ ./xx.rb
[#<struct Check
name=“Check email”,
results=[“DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08
11:12:35”]>]
$ cat xx.rb
#!/bin/ruby

Check = Struct.new :name, :results

content = DATA.read
items = []
item = nil

content.each_line do |line|
line.chomp!

case line
when /^\s*([\w\s]+?)\s*:\s*$/
items << item if item
item = Check.new $1, []
when /^\s*$/
# ignore
else
item.results << line if item
end
end

items << item if item
items.reject! {|it| it.results.empty?}

require ‘pp’
pp items

END
Check Fault Check :

Check Chassis fault :

Check email :
DISK WARNING - usage: C: 36797 MB (90%) 2013-10-08 11:12:35

Check x:

$

Kind regards

robert