[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Group several lines into one line

Dirk Dre

4/27/2009 6:26:00 PM

After sitting at this problem for hours without much progress, now is
the time that I need your help. Btw. I'm quite new to ruby.

I have a file that looks like this:

00-04-00;Austragungssystem für längliche oder
00-04-00;quadratische Lagerräume inklusive 3-
00-04-00;poligen Wielandstecker/Gegenstecker
00-04-00;Technische Daten:
00-04-00;Elektrischer Anschluss: 230V / 50Hz
00002274;Wilo Temperaturfühler TF
00002274;Temperaturschalter mit Einstellknopf
00002274;einschlie�lich 2 Stück Federspannbändern
00002274;zum Anlegen an Rohre bis DN 100.
00002274;Max. Betriebsspannung: 250 V
00002274;Max. Schaltleistung: 4 A
00002274;Schutzart: IP 43
00002274;Schaltbereich: 30 oC bis 90 oC
00002274;Fabrikat: WILO
00002274;Typ: Temperaturschalter TF

what i need is to group lines with the same serialnumber
(eg. 00-04-00, 00002274) into one line like this:

00-04-00;Austragungssystem für längliche oder quadratische Lagerräume
inklusive 3-poligen Wielandstecker/Gegenstecker Technische Daten:
Elektrischer Anschluss: 230V / 50Hz

I need several lines combined to one line with the matching serialnumber
in front.

I searched for several hours on the internet and in several forums.
Maybe I was looking for the wrong search terms. But I'm at a loss here.

Thanks in advance.
--
Posted via http://www.ruby-....

6 Answers

Michael Furmaniuk

4/27/2009 6:52:00 PM

0

Dirk Dre wrote:
> I need several lines combined to one line with the matching serialnumber
> in front.
I'm not good at coming up with the code examples off the top of my head
but maybe a regex matching the serial numbers and then a split to pull
off the portion you want then combining the matched portion.
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

4/27/2009 7:00:00 PM

0

On Mon, Apr 27, 2009 at 8:25 PM, Dirk Dre <dad@pulf.de> wrote:
> After sitting at this problem for hours without much progress, now is
> the time that I need your help. Btw. I'm quite new to ruby.
>
> I have a file that looks like this:
>
> 00-04-00;Austragungssystem f=FCr l=E4ngliche oder
> 00-04-00;quadratische Lagerr=E4ume inklusive 3-
> 00-04-00;poligen Wielandstecker/Gegenstecker
> 00-04-00;Technische Daten:
> 00-04-00;Elektrischer Anschluss: 230V / 50Hz
> 00002274;Wilo Temperaturf=FChler TF
> 00002274;Temperaturschalter mit Einstellknopf
> 00002274;einschlie=DFlich 2 St=FCck Federspannb=E4ndern
> 00002274;zum Anlegen an Rohre bis DN 100.
> 00002274;Max. Betriebsspannung: 250 V
> 00002274;Max. Schaltleistung: 4 A
> 00002274;Schutzart: IP 43
> 00002274;Schaltbereich: 30 oC bis 90 oC
> 00002274;Fabrikat: WILO
> 00002274;Typ: Temperaturschalter TF
>
> what i need is to group lines with the same serialnumber
> (eg. 00-04-00, 00002274) into one line like this:
>
> 00-04-00;Austragungssystem f=FCr l=E4ngliche oder quadratische Lagerr=E4u=
me
> inklusive 3-poligen Wielandstecker/Gegenstecker Technische Daten:
> Elektrischer Anschluss: 230V / 50Hz


Something like this (untested) might get you started:

s =3D "00-40..." #your string
h =3D Hash.new {|h,k| h[k] =3D ""}
s.each do |line|
key, value =3D line.split(";")
h[key] << value.chomp
end

This will give you a hash where the keys are the serial numbers and
the values, the concatenated parts that correspond to that serial
number.

Jesus.

Joel VanderWerf

4/27/2009 7:22:00 PM

0

Jesús Gabriel y Galán wrote:
> On Mon, Apr 27, 2009 at 8:25 PM, Dirk Dre <dad@pulf.de> wrote:
>> After sitting at this problem for hours without much progress, now is
>> the time that I need your help. Btw. I'm quite new to ruby.
>>
>> I have a file that looks like this:
>>
>> 00-04-00;Austragungssystem für längliche oder
>> 00-04-00;quadratische Lagerräume inklusive 3-
>> 00-04-00;poligen Wielandstecker/Gegenstecker
>> 00-04-00;Technische Daten:
>> 00-04-00;Elektrischer Anschluss: 230V / 50Hz
>> 00002274;Wilo Temperaturfühler TF
>> 00002274;Temperaturschalter mit Einstellknopf
>> 00002274;einschließlich 2 Stück Federspannbändern
>> 00002274;zum Anlegen an Rohre bis DN 100.
>> 00002274;Max. Betriebsspannung: 250 V
>> 00002274;Max. Schaltleistung: 4 A
>> 00002274;Schutzart: IP 43
>> 00002274;Schaltbereich: 30 oC bis 90 oC
>> 00002274;Fabrikat: WILO
>> 00002274;Typ: Temperaturschalter TF
>>
>> what i need is to group lines with the same serialnumber
>> (eg. 00-04-00, 00002274) into one line like this:
>>
>> 00-04-00;Austragungssystem für längliche oder quadratische Lagerräume
>> inklusive 3-poligen Wielandstecker/Gegenstecker Technische Daten:
>> Elektrischer Anschluss: 230V / 50Hz
>
>
> Something like this (untested) might get you started:
>
> s = "00-40..." #your string
> h = Hash.new {|h,k| h[k] = ""}
> s.each do |line|
> key, value = line.split(";")
> h[key] << value.chomp
> end

That won't quite work if there is a second semicolon on the line. Try:

key, value = line.scan(/([^;]*);(.*)/).first

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Rob Biedenharn

4/27/2009 7:36:00 PM

0


On Apr 27, 2009, at 2:59 PM, Jes=FAs Gabriel y Gal=E1n wrote:

> On Mon, Apr 27, 2009 at 8:25 PM, Dirk Dre <dad@pulf.de> wrote:
>> After sitting at this problem for hours without much progress, now is
>> the time that I need your help. Btw. I'm quite new to ruby.
>>
>> I have a file that looks like this:
>>
>> 00-04-00;Austragungssystem f=FCr l=E4ngliche oder
>> 00-04-00;quadratische Lagerr=E4ume inklusive 3-
>> 00-04-00;poligen Wielandstecker/Gegenstecker
>> 00-04-00;Technische Daten:
>> 00-04-00;Elektrischer Anschluss: 230V / 50Hz
>> 00002274;Wilo Temperaturf=FChler TF
>> 00002274;Temperaturschalter mit Einstellknopf
>> 00002274;einschlie=DFlich 2 St=FCck Federspannb=E4ndern
>> 00002274;zum Anlegen an Rohre bis DN 100.
>> 00002274;Max. Betriebsspannung: 250 V
>> 00002274;Max. Schaltleistung: 4 A
>> 00002274;Schutzart: IP 43
>> 00002274;Schaltbereich: 30 oC bis 90 oC
>> 00002274;Fabrikat: WILO
>> 00002274;Typ: Temperaturschalter TF
>>
>> what i need is to group lines with the same serialnumber
>> (eg. 00-04-00, 00002274) into one line like this:
>>
>> 00-04-00;Austragungssystem f=FCr l=E4ngliche oder quadratische =
Lagerr=E4ume
>> inklusive 3-poligen Wielandstecker/Gegenstecker Technische Daten:
>> Elektrischer Anschluss: 230V / 50Hz
>
>
> Something like this (untested) might get you started:
>
> s =3D "00-40..." #your string
> h =3D Hash.new {|h,k| h[k] =3D ""}
> s.each do |line|
> key, value =3D line.split(";")
> h[key] << value.chomp
> end
>
> This will give you a hash where the keys are the serial numbers and
> the values, the concatenated parts that correspond to that serial
> number.
>
> Jesus.


current_serial =3D nil
texts =3D []
File.open(outputfilename, 'w') do |out|
File.foreach(filename) do |line|
serial, text =3D line.chomp.split(';', 2)
if current_serial && serial !=3D current_serial
out.puts "#{current_serial};#{texts.join(' ')}"
texts =3D []
end
current_serial =3D serial
texts << text
end
if current_serial
out.puts "#{current_serial};#{texts.join(' ')}"
end
end

Two things of note: The second argument to split(';', 2) limits the =20
result to 2 items so if there happens to be a ';' later in the line it =20=

isn't considered a place to split. Keeping the items in a hash =20
doesn't guarantee the order on the way back out, but I'm also assuming =20=

that all the lines with a given serial number are together.

You have to initialize your filename and outputfilename, of course.
-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Dirk Dre

4/28/2009 12:38:00 AM

0

Thanks a lot to all.

Rob,
your solution works like a charme.

Now i'll just do some heavy commenting, to understand it all and for
reference.
--
Posted via http://www.ruby-....

Robert Schaaf

5/1/2009 1:04:00 AM

0

The desired output cannot be achieved unless your log preserves =20
trailing spaces. Thus:

00002274;Temperaturschalter mit Einstellknopfeinschlie=DFlich 2 St=FCck =20=

Federspannb=E4ndernzum Anlegen an Rohre bis DN 100.Max. =20
Betriebsspannung: 250 VMax. Schaltleistung: 4 ASchutzart: IP =20
43Schaltbereich: 30 oC bis 90 oCFabrikat: WILOTyp: Temperaturschalter TF

My suggestion is to pad with a space unless the line ends in a hyphen.

Picayune perhaps, but aesthetics (readability) counts.

Bob Schaaf


On Apr 27, 2009, at 3:36 PM, Rob Biedenharn wrote:

>
> On Apr 27, 2009, at 2:59 PM, Jes=FAs Gabriel y Gal=E1n wrote:
>
>> On Mon, Apr 27, 2009 at 8:25 PM, Dirk Dre <dad@pulf.de> wrote:
>>> After sitting at this problem for hours without much progress, now =20=

>>> is
>>> the time that I need your help. Btw. I'm quite new to ruby.
>>>
>>> I have a file that looks like this:
>>>
>>> 00-04-00;Austragungssystem f=FCr l=E4ngliche oder
>>> 00-04-00;quadratische Lagerr=E4ume inklusive 3-
>>> 00-04-00;poligen Wielandstecker/Gegenstecker
>>> 00-04-00;Technische Daten:
>>> 00-04-00;Elektrischer Anschluss: 230V / 50Hz
>>> 00002274;Wilo Temperaturf=FChler TF
>>> 00002274;Temperaturschalter mit Einstellknopf
>>> 00002274;einschlie=DFlich 2 St=FCck Federspannb=E4ndern
>>> 00002274;zum Anlegen an Rohre bis DN 100.
>>> 00002274;Max. Betriebsspannung: 250 V
>>> 00002274;Max. Schaltleistung: 4 A
>>> 00002274;Schutzart: IP 43
>>> 00002274;Schaltbereich: 30 oC bis 90 oC
>>> 00002274;Fabrikat: WILO
>>> 00002274;Typ: Temperaturschalter TF
>>>
>>> what i need is to group lines with the same serialnumber
>>> (eg. 00-04-00, 00002274) into one line like this:
>>>
>>> 00-04-00;Austragungssystem f=FCr l=E4ngliche oder quadratische =20
>>> Lagerr=E4ume
>>> inklusive 3-poligen Wielandstecker/Gegenstecker Technische Daten:
>>> Elektrischer Anschluss: 230V / 50Hz
>>
>>
>> Something like this (untested) might get you started:
>>
>> s =3D "00-40..." #your string
>> h =3D Hash.new {|h,k| h[k] =3D ""}
>> s.each do |line|
>> key, value =3D line.split(";")
>> h[key] << value.chomp
>> end
>>
>> This will give you a hash where the keys are the serial numbers and
>> the values, the concatenated parts that correspond to that serial
>> number.
>>
>> Jesus.
>
>
> current_serial =3D nil
> texts =3D []
> File.open(outputfilename, 'w') do |out|
> File.foreach(filename) do |line|
> serial, text =3D line.chomp.split(';', 2)
> if current_serial && serial !=3D current_serial
> out.puts "#{current_serial};#{texts.join(' ')}"
> texts =3D []
> end
> current_serial =3D serial
> texts << text
> end
> if current_serial
> out.puts "#{current_serial};#{texts.join(' ')}"
> end
> end
>
> Two things of note: The second argument to split(';', 2) limits the =20=

> result to 2 items so if there happens to be a ';' later in the line =20=

> it isn't considered a place to split. Keeping the items in a hash =20
> doesn't guarantee the order on the way back out, but I'm also =20
> assuming that all the lines with a given serial number are together.
>
> You have to initialize your filename and outputfilename, of course.
> -Rob
>
> Rob Biedenharn http://agileconsult...
> Rob@AgileConsultingLLC.com
>
>
>