Create text files

Hi,

I do have a text file as shown below. I'd now like to use Ruby to create
separate text files for the content between "======". To do so I have:

# Open, read from a text file, and pass to block f1
File.open('test.txt', 'r') do |f1|
  while line = f1.gets
     puts line
  end
end

# Create a new file and write to it
File.open('cve.txt', 'w+') do |f2|
   if f2 =~ /Name/
     puts line
   end
end

Though cve.txt is empty and I just have one cve.txt. Pls advise. I
appreciate.

Test.txt

···

======================================================
Name: CVE-1999-0005
Status: Entry
Reference: CERT:CA-98.09.imapd
Reference: SUN:00177
Reference: URL:
http://sunsolve.sun.com/pub-cgi/retrieve.pl?doctype=coll&doc=secbull/177
Reference: BID:130
Reference: URL:http://www.securityfocus.com/bid/130
Reference: XF:imap-authenticate-bo

Arbitrary command execution via IMAP buffer overflow in authenticate
command.

======================================================
Name: CVE-1999-0006
Status: Entry
Reference: CERT:CA-98.08.qpopper_vul
Reference: SGI:19980801-01-I
Reference: URL:
ftp://patches.sgi.com/support/free/security/advisories/19980801-01-I
Reference: AUSCERT:AA-98.01
Reference: XF:qpopper-pass-overflow
Reference: BID:133
Reference: URL:http://www.securityfocus.com/bid/133

Buffer overflow in POP servers based on BSD/Qualcomm's qpopper allows
remote attackers to gain root access using a long PASS command.

======================================================

Dear Tom,

you could do the following:

all_text=IO.readlines("test.txt").to_s.split(/\=+/)
name_regexp=/Name: ([^\n]+)/
all_text.each{|entry_between_equal_signs|
      file_name=name_regexp.match(entry_between_equal_signs)
      if file_name!=nil and File.exists?(file_name[1]+'.txt')==false
        f=File.new(file_name[1]+'.txt','w')
        f.puts entry_between_equal_signs
        f.close
      end
}

Best regards,

Axel

-------- Original-Nachricht --------

···

Datum: Fri, 15 Jun 2007 16:53:53 +0900
Von: "Tom Bombadil" <bombadil.tom@gmail.com>
An: ruby-talk@ruby-lang.org
Betreff: Create text files...

Hi,

I do have a text file as shown below. I'd now like to use Ruby to create
separate text files for the content between "======". To do so I have:

# Open, read from a text file, and pass to block f1
File.open('test.txt', 'r') do |f1|
  while line = f1.gets
     puts line
  end
end

# Create a new file and write to it
File.open('cve.txt', 'w+') do |f2|
   if f2 =~ /Name/
     puts line
   end
end

Though cve.txt is empty and I just have one cve.txt. Pls advise. I
appreciate.

Test.txt

Name: CVE-1999-0005
Status: Entry
Reference: CERT:CA-98.09.imapd
Reference: SUN:00177
Reference: URL:
http://sunsolve.sun.com/pub-cgi/retrieve.pl?doctype=coll&doc=secbull/177
Reference: BID:130
Reference: URL:http://www.securityfocus.com/bid/130
Reference: XF:imap-authenticate-bo

Arbitrary command execution via IMAP buffer overflow in authenticate
command.

======================================================
Name: CVE-1999-0006
Status: Entry
Reference: CERT:CA-98.08.qpopper_vul
Reference: SGI:19980801-01-I
Reference: URL:
ftp://patches.sgi.com/support/free/security/advisories/19980801-01-I
Reference: AUSCERT:AA-98.01
Reference: XF:qpopper-pass-overflow
Reference: BID:133
Reference: URL:http://www.securityfocus.com/bid/133

Buffer overflow in POP servers based on BSD/Qualcomm's qpopper allows
remote attackers to gain root access using a long PASS command.

======================================================

--
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: GMX E-Mail ✉ sichere & kostenlose E-Mail-Adresse ✉

not ruby, but...

$ csplit -f cve -z -n4 -k test.txt '/===================/' '{99999}'

$ ls -rlt cve*
-rw-r--r-- 1 rthompso staff 56 Jun 15 08:14 cve0003
-rw-r--r-- 1 rthompso staff 511 Jun 15 08:14 cve0002
-rw-r--r-- 1 rthompso staff 416 Jun 15 08:14 cve0001
-rw-r--r-- 1 rthompso staff 9 Jun 15 08:14 cve0000

$for file in `ls cve*`
do
echo $file
echo "begin file output"
cat $file
echo "end file output"
echo ""
done

cve0000
begin file output
Test.txt
end file output

cve0001
begin file output

···

On Fri, 2007-06-15 at 16:53 +0900, Tom Bombadil wrote:

Hi,

I do have a text file as shown below. I'd now like to use Ruby to create
separate text files for the content between "======". To do so I have:

# Open, read from a text file, and pass to block f1
File.open('test.txt', 'r') do |f1|
  while line = f1.gets
     puts line
  end
end

# Create a new file and write to it
File.open('cve.txt', 'w+') do |f2|
   if f2 =~ /Name/
     puts line
   end
end

Though cve.txt is empty and I just have one cve.txt. Pls advise. I
appreciate.

Test.txt

Name: CVE-1999-0005
Status: Entry
Reference: CERT:CA-98.09.imapd
Reference: SUN:00177
Reference: URL:
http://sunsolve.sun.com/pub-cgi/retrieve.pl?doctype=coll&doc=secbull/177
Reference: BID:130
Reference: URL:http://www.securityfocus.com/bid/130
Reference: XF:imap-authenticate-bo

Arbitrary command execution via IMAP buffer overflow in authenticate
command.

======================================================
Name: CVE-1999-0006
Status: Entry
Reference: CERT:CA-98.08.qpopper_vul
Reference: SGI:19980801-01-I
Reference: URL:
ftp://patches.sgi.com/support/free/security/advisories/19980801-01-I
Reference: AUSCERT:AA-98.01
Reference: XF:qpopper-pass-overflow
Reference: BID:133
Reference: URL:http://www.securityfocus.com/bid/133

Buffer overflow in POP servers based on BSD/Qualcomm's qpopper allows
remote attackers to gain root access using a long PASS command.

======================================================

======================================================
Name: CVE-1999-0005
Status: Entry
Reference: CERT:CA-98.09.imapd
Reference: SUN:00177
Reference: URL:
http://sunsolve.sun.com/pub-cgi/retrieve.pl?doctype=coll&doc=secbull/177
Reference: BID:130
Reference: URL:http://www.securityfocus.com/bid/130
Reference: XF:imap-authenticate-bo

Arbitrary command execution via IMAP buffer overflow in authenticate
command.

end file output

cve0002
begin file output

Name: CVE-1999-0006
Status: Entry
Reference: CERT:CA-98.08.qpopper_vul
Reference: SGI:19980801-01-I
Reference: URL:
ftp://patches.sgi.com/support/free/security/advisories/19980801-01-I
Reference: AUSCERT:AA-98.01
Reference: XF:qpopper-pass-overflow
Reference: BID:133
Reference: URL:http://www.securityfocus.com/bid/133

Buffer overflow in POP servers based on BSD/Qualcomm's qpopper allows
remote attackers to gain root access using a long PASS command.

end file output

cve0003
begin file output

end file output