Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2|3lines

0 views
Skip to first unread message

Moody

unread,
Nov 23, 2009, 8:44:18 AM11/23/09
to
hi,

Input:

Component Part # Serial Description Date Time Vend
Location
--------- ------ ------ ----------- ---- ---- ----
--------
SSC0 501-5407-14-59 038298 System Cont 09/22 05:21:53 012c
Sriracha,
roller 2004 GMT+05:00
Chonburi,

Thailand
ID0 501-4406-07-50 904882 Sun Fire 48 06/30 02:35:54 03c1
Juarez Me
00 Centerpl 2004 GMT+05:00
xico
ane
PS0 300-1460-03-52 C13906 Power Suppl 08/14 23:45:01 031a
Matamoros
y (A153, F4 2002 GMT
+05:00 , Tamps,
800)
Mexico
PS1 300-1460-04-50 W17035 Power Suppl 03/22 16:05:41 026d
FDK CHINA
y (A153, F4 2004 GMT+05:00
800)
FT0 540-4345-01-52 LK0040 Fan Tray (F 11/24 13:37:14 021c
Lisburn
4800, Botto 2004 GMT+05:00
m I/O)
FT1 540-4344-02-52 LJ00ME Fan Tray (F 10/20 14:16:04 021c
Sanmina-S
4800, Top I 2004 GMT+05:00
CI Lisbur
/O)
n
FT2 540-3586-01-52 LG00M7 Fan Tray (F 10/20 14:51:19 021c
Sanmina-S
4800, CPU) 2004 GMT+05:00
CI, Lisb

urn
RP0 501-6418-05-51 021975 Repeater Bo 08/23 08:52:31 012c
Sriracha,
ard 2004 GMT+05:00
Chonburi,

Thailand
RP2 501-6418-05-51 022243 Repeater Bo 08/30 21:23:02 012c
Sriracha,
ard 2004 GMT+05:00
Chonburi,

Thailand
/N0/SB0 501-7539-02-50 A00386 CPU Board V 01/14 18:18:02 0301
Penang, M
2 2007 GMT+05:00
alaysia
/N0/SB2 501-6178-03-51 A38545 CPU Board V 01/28 02:35:05 0301
Penang, M
2 2004 GMT+05:00
alaysia
/N0/SB0/P2/B0/D0 501-5030-03-50 329361 512 MB NG S 10/28 18:59:36 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P2/B0/D1 501-5030-03-50 334926 512 MB NG S 10/28 19:33:12 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P2/B0/D2 501-5030-03-50 334931 512 MB NG S 10/28 19:33:12 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P2/B0/D3 501-5030-03-50 329682 512 MB NG S 10/28 22:55:48 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P3/B0/D0 501-5030-03-50 270045 512 MB NG S 10/29 19:18:49 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P3/B0/D1 501-5030-03-50 270871 512 MB NG S 10/29 20:02:57 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P3/B0/D2 501-5030-03-50 270014 512 MB NG S 10/29 19:27:57 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P3/B0/D3 501-5030-03-50 270022 512 MB NG S 10/29 19:27:57 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P2/B0/D0 501-5030-03-50 328988 512 MB NG S 10/28 17:20:46 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P2/B0/D1 501-5030-03-50 334841 512 MB NG S 10/28 16:53:11 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P2/B0/D2 501-5030-03-50 334875 512 MB NG S 10/28 16:58:57 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P2/B0/D3 501-5030-03-50 329489 512 MB NG S 10/28 17:15:35 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P3/B0/D0 501-5030-03-50 328956 512 MB NG S 10/28 16:34:08 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P3/B0/D1 501-5030-03-50 334991 512 MB NG S 10/28 16:58:57 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P3/B0/D2 501-5030-03-50 270196 512 MB NG S 10/29 17:54:51 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB2/P3/B0/D3 501-5030-03-50 270181 512 MB NG S 10/29 17:54:51 00ce
ONYANG,KO
DRAM DIMM 2004 GMT+05:00
REA
/N0/SB0/P2/E0 371-1594-01-01 0D2540 4MB Ecache 12/13 10:02:37 00ce
Celestica
Module 2006 GMT
+05:00 Toronto,

Canada
/N0/SB0/P2/E1 371-1594-01-01 0D2HV9 4MB Ecache 12/13 10:04:08 00ce
Celestica
Module 2006 GMT
+05:00 Toronto,

Canada
/N0/SB0/P3/E0 371-1594-01-01 0D2ZM1 4MB Ecache 12/13 10:56:28 00ce
Celestica
Module 2006 GMT
+05:00 Toronto,

Canada
/N0/SB0/P3/E1 371-1594-01-01 0D2ZEH 4MB Ecache 12/13 10:55:14 00ce
Celestica
Module 2006 GMT
+05:00 Toronto,

Canada
/N0/SB2/P2/E0 370-4128-03-50 1B9D2K 4MB Ecache 01/08 02:38:47 00ce
Celestica
Module 2004 GMT
+05:00 Toronto,

Canada
/N0/SB2/P2/E1 370-4128-03-50 1B91GM 4MB Ecache 01/08 02:38:40 00ce
Celestica
Module 2004 GMT
+05:00 Toronto,

Canada
/N0/SB2/P3/E0 370-4128-03-50 1B91GN 4MB Ecache 01/08 02:38:43 00ce
Celestica
Module 2004 GMT
+05:00 Toronto,

Canada
/N0/SB2/P3/E1 370-4128-03-50 1B8XUX 4MB Ecache 01/08 02:38:45 00ce
Celestica
Module 2004 GMT
+05:00 Toronto,

Canada
/N0/IB6 501-4404-13-51 054123 PCI I/O Ass 02/22 09:32:47 012c
Celestica
embly 2004 GMT
+05:00 ,Toronto,

Ontario
/N0/IB8 501-4404-13-51 054219 PCI I/O Ass 02/25 08:42:03 012c
Celestica
embly 2004 GMT
+05:00 ,Toronto,

Ontario

above output is a multilined columns and a maximum of 3 lines are
there .

Output.

I need to them all in a single line with correct column values
appended. like last line will be like.

/N0/IB8 501-4404-13-51 054219 PCI I/O Assembly 02/25 2004 08:42:03 GMT
+05:00 012c Celestica,Toronto,Ontario

This is all one line instead of multiple lines.

Regards,
BB.

Ben Bacarisse

unread,
Nov 23, 2009, 9:09:00 AM11/23/09
to
Moody <nasir....@gmail.com> writes:

> Input:

<I'll snip most of it>


> /N0/IB8 501-4404-13-51 054219 PCI I/O Ass 02/25 08:42:03 012c
> Celestica
> embly 2004 GMT
> +05:00 ,Toronto,
>
> Ontario
>
> above output is a multilined columns and a maximum of 3 lines are
> there .
>
> Output.
>
> I need to them all in a single line with correct column values
> appended. like last line will be like.
>
> /N0/IB8 501-4404-13-51 054219 PCI I/O Assembly 02/25 2004 08:42:03 GMT
> +05:00 012c Celestica,Toronto,Ontario
>
> This is all one line instead of multiple lines.

Your post has had its lines broken so it is very hard to tell exactly
what it going on, but I think you'll need something like a Perl script
to do this. Some information has been lost so it will be hard to get
exactly the right output. For example, "Ass" and "embly" must be
joined with no space, but "08:42:03" and "GMT" require one.

I don't have time to write it for you, but the gist is to keep a "last
line" array of fields that you output when you see a line that starts
with a non-space. Lines that start with a space are broken up into
fixed size fields and these are added to the corresponding fields of
the "last line".

I am sure it could be done in awk but there is enough that is not a
natural fit that I'd go for Perl.

--
Ben.

Ed Morton

unread,
Nov 23, 2009, 10:22:10 AM11/23/09
to
On Nov 23, 8:09 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:

No, it's a very natural fit for awk and since it's just text
processing it doesn't need any of the additional functionality that
perl provides.

For the OP - try posting a small set of representative input that
doesn't wrap around lines since that's what anyone who wants to help
you will need to come up with a solution so it'd make more sense for
you to put the effort into that than everyone who wants to help you.
Also tell us what the spaces are between fields (blanks? tabs?
something else?)

Ed

Loki Harfagr

unread,
Nov 23, 2009, 1:09:41 PM11/23/09
to
Mon, 23 Nov 2009 05:44:18 -0800, Moody did cat :

> /N0/IB6 501-4404-13-51 054123 PCI I/O Ass 02/22 09:32:47 012c
> Celestica
> embly 2004 GMT
> +05:00 ,Toronto,
>
> Ontario

> /N0/IB8 *-4404-13-51 054219 PCI I/O Ass 02/25 08:42:03 012c


> Celestica
> embly 2004 GMT
> +05:00 ,Toronto,
>
> Ontario
>
> above output is a multilined columns and a maximum of 3 lines are there
> .
>
> Output.
>
> I need to them all in a single line with correct column values appended.
> like last line will be like.
>
> /N0/IB8 501-4404-13-51 054219 PCI I/O Assembly 02/25 2004 08:42:03 GMT
> +05:00 012c Celestica,Toronto,Ontario
>
> This is all one line instead of multiple lines.

Interesting...

As from here it looks much worse than just 'one instead of mult'
would you please post a shorter sample data with shorter fields
that wouldn't wrap when posted!

(what I see here is b0tched stuff like:
-----------
/N0/IB8 'some num refs' 'start of label' 'date'
'part of address'
[blanks] 'end of label' 'start of time'
'end of time' , 'start of city/state'
<blank line>
'end of city/state'
-----------
)

and if your data really is what it looks here you may need a boar
a goat and several chickens sacrified in front of the printer ,-)

Ben Bacarisse

unread,
Nov 23, 2009, 2:20:44 PM11/23/09
to
Ed Morton <morto...@gmail.com> writes:

> On Nov 23, 8:09 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>> Moody <nasir.mahm...@gmail.com> writes:
>> > Input:
>>
>> <I'll snip most of it>
>> > /N0/IB8          501-4404-13-51 054219 PCI I/O Ass 02/25 08:42:03 012c
>> > Celestica
>> >                                        embly       2004  GMT
>> > +05:00     ,Toronto,
>> > Ontario

<snip>


>> I am sure it could be done in awk but there is enough that is not a
>> natural fit that I'd go for Perl.
>
> No, it's a very natural fit for awk and since it's just text
> processing it doesn't need any of the additional functionality that
> perl provides.

That's a reasonable use of "natural" but I was simply using it to mean
that awk "does too much". There no gain from awk's automatic
splitting of lines here.

Why not compare? Here is a stab at it in Perl. It is fiddly due
to the spacing issues I mentioned and Perl's map and slice do come
into play (though they can of course be avoided).

$_ = <>; $_ = <>;
my @r;
while (<>) {
my @f = /(.{17})(.{15})(.{7})(.{1,11}) ?(.{0,6})(.{0,9})(.{0,5})(.*)/;
map { s/ *$// } @f[0..3,6];
if ($f[0] eq '') {
map { s/ *$// } @f[4,5];
my $i = 0;
map { $_ .= $f[$i++] } @r;
} else {
print join(' ', @r), "\n" if @r;
@r = @f;
}
}
print join(' ', @r), "\n" if @r;

I had to make a guess at how much damage had happened to the posted
data so some of the details may be wrong.

--
Ben.

Ed Morton

unread,
Nov 23, 2009, 5:49:40 PM11/23/09
to
On Nov 23, 1:20 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Ben.- Hide quoted text -
>
> - Show quoted text -

Once the OP posts some more reasonably formatted input and some more
info I'll have a stab at it in awk. Right now personally I'm really
not sure enough of what he's looking for to even begin, but I am
fairly confident that his next couple of posts will be along the lines
of "thanks, but my input REALLY looks like...." and I'd rather not be
the one he's responding to with that!

Ed.

Loki Harfagr

unread,
Nov 24, 2009, 2:20:15 AM11/24/09
to
Mon, 23 Nov 2009 14:09:00 +0000, Ben Bacarisse did cat :

> Moody <nasir....@gmail.com> writes:
>
>> Input:
>
> <I'll snip most of it>

ditto ;-)

>> /N0/IB8 501-4404-13-51 054219 PCI I/O Ass 02/25 08:42:03 012c
>> Celestica
>> embly 2004 GMT
>> +05:00 ,Toronto,

...


>> This is all one line instead of multiple lines.
>
> Your post has had its lines broken so it is very hard to tell exactly
> what it going on, but I think you'll need something like a Perl script
> to do this.

was that the reason why you broke the multipost and threw the cl.awk out?~>

Hermann Peifer

unread,
Nov 24, 2009, 2:48:49 AM11/24/09
to
Ben Bacarisse wrote:
>
> That's a reasonable use of "natural" but I was simply using it to mean
> that awk "does too much". There no gain from awk's automatic
> splitting of lines here.
>
> Why not compare? Here is a stab at it in Perl. It is fiddly due
> to the spacing issues I mentioned and Perl's map and slice do come
> into play (though they can of course be avoided).
>
> $_ = <>; $_ = <>;
> my @r;
> while (<>) {
> my @f = /(.{17})(.{15})(.{7})(.{1,11}) ?(.{0,6})(.{0,9})(.{0,5})(.*)/;
> map { s/ *$// } @f[0..3,6];
> if ($f[0] eq '') {
> map { s/ *$// } @f[4,5];
> my $i = 0;
> map { $_ .= $f[$i++] } @r;
> } else {
> print join(' ', @r), "\n" if @r;
> @r = @f;
> }
> }
> print join(' ', @r), "\n" if @r;
>
> I had to make a guess at how much damage had happened to the posted
> data so some of the details may be wrong.
>

Here my guess about how the original data might look like: multi-line
records separated by an empty line, each record has 8 fix-width columns

SSC0 501-5407-14-59 038298 System Cont 09/22 05:21:53 012c
Sriracha,
roller 2004 GMT+05:00
Chonburi,

Thailand

ID0 501-4406-07-50 904882 Sun Fire 48 06/30 02:35:54 03c1
Juarez Me
00 Centerpl 2004 GMT+05:00 xico
ane

/N0/IB8 501-4404-13-51 054219 PCI I/O Ass 02/25 08:42:03 012c

Celestica
embly 2004 GMT+05:00
,Toronto,

Ontario


Here a script, which uses several gawk extensions:

$ cat moody.awk

BEGIN { FIELDWIDTHS = "17 15 7 12 6 9 5 9" }

NF {
for (i = 1; i <= NF; i++) {
a[i] = a[i] s[i] trim($i)
s[i] = i == 5 || i == 6 ? " " : ""
}
next
}

{
for (i = 1; i <= length(a); i++)
printf "|%s", trim(a[i])

print "|"
delete a
delete s
}

function trim(str) {
sub(/^[[:space:]]+/, "", str)
if (str) sub(/[[:space:]]+$/, "", str)
return str
}


$ gawk -f moody.awk testdata
|SSC0|501-5407-14-59|038298|System Controller|09/22 2004|05:21:53
GMT+05:00|012c|Sriracha,Chonburi,Thailand|
|ID0|501-4406-07-50|904882|Sun Fire 4800 Centerplane|06/30 2004|02:35:54
GMT+05:00|03c1|Juarez Mexico|
|/N0/IB8|501-4404-13-51|054219|PCI I/O Assembly|02/25 2004|08:42:03
GMT+05:00|012c|Celestica,Toronto,Ontario|

Ed Morton

unread,
Nov 24, 2009, 8:58:50 AM11/24/09
to

That input still wraps on my screen so I still can't tell for sure what it's
supposed to be and I couldn't get the script to produce that output and couldn't
be bothered to debug the script or input file, so let';s try getting some
representative input that fits on a screen. Keeping just the interesting fields,
I THINK it's supposed to be something like this:

#######################################
Component Description Time Location
--------- ----------- ---- --------
SSC0 System Cont 05:21:53 Sriracha,
roller GMT+05:00Chonburi,
Thailand

ID0 Sun Fire 48 02:35:54 Juarez Me
00 Centerpl GMT+05:00xico
ane

/N0/IB8 PCI I/O Ass 08:42:03 Celestica
embly GMT+05:00,Toronto,
Ontario
########################################

with desired output:

########################################
SSC0|System Controller|09/22 2004|05:21:53 GMT+05:00|Sriracha,Chonburi,Thailand
ID0|Sun Fire 4800 Centerplane|02:35:54 GMT+05:00|Juarez Mexico
/N0/IB8|PCI I/O Assembly|08:42:03 GMT+05:00|Celestica,Toronto,Ontario
########################################

Right?

Ed.

Ed Morton

unread,
Nov 24, 2009, 9:01:50 AM11/24/09
to

Should be:

########################################
SSC0|System Controller|05:21:53 GMT+05:00|Sriracha,Chonburi,Thailand


ID0|Sun Fire 4800 Centerplane|02:35:54 GMT+05:00|Juarez Mexico
/N0/IB8|PCI I/O Assembly|08:42:03 GMT+05:00|Celestica,Toronto,Ontario
########################################

sigh...

> Right?
>
> Ed.

Ed Morton

unread,
Nov 24, 2009, 9:58:41 AM11/24/09
to

OK, for the purposes of winding up this comparison thread: given this input file
(which I tweaked a bit from my previous post to include a case I hadn't thought
of previously but I'm certain must show up in the OPs real input somewhere),
output, and awk script:

$ cat file
Component Description Time Location
--------- ----------- ---- --------


SSC0 System Cont 05:21:53 Sriracha,
roller GMT+05:00Chonburi,
Thailand

ID0 Sun Fire 48 02:35:54 Juarez Me
00 Centerpl GMT+05:00xico
ane

/N0/IB8 PCI I/O Big 08:42:03 Celestica
Part GMT+05:00,Toronto,
Ontario

$ awk -f ed.awk file


SSC0|System Controller|05:21:53 GMT+05:00|Sriracha,Chonburi,Thailand
ID0|Sun Fire 4800 Centerplane|02:35:54 GMT+05:00|Juarez Mexico

/N0/IB8|PCI I/O Big Part|08:42:03 GMT+05:00|Celestica,Toronto,Ontario
$ cat ed.awk
BEGIN { FIELDWIDTHS = "9 1 11 1 9 9"; skipFlds="^(2|4)$" }
NR>2 {
if (NF) {
numFlds=0


for (i = 1; i <= NF; i++) {

if (i !~ skipFlds) {
numFlds++
fld[numFlds] = fld[numFlds] $i
}
}
} else {
rec=sep=""
for (i = 1; i <= numFlds; i++) {
sub(/[[:space:]]+$/,"",fld[i])
rec = rec sep fld[i]
sep = "|"
}
print rec
delete fld
}
}

feel free to post the modified version of your perl script to use the adjusted
field numbers/lengths if you like.

Regards,

Ed.

Hermann Peifer

unread,
Nov 24, 2009, 10:29:16 AM11/24/09
to


Right. Here the slightly modified script that produces the above output.
I guess you can make it with substantially less than code. I also
cheated somewhat as the script expects an empty line at the end of the
data file

Hermann


BEGIN { FIELDWIDTHS = "10 12 9 9" }

NR < 3 { next }

NF {
for (i = 1; i <= NF; i++) {
a[i] = a[i] s[i] trim($i)

s[i] = i == 3 ? " " : ""
}
next
}

{
n = length(a)
for (i = 1; i <= n; i++) {
printf "%s%s", trim(a[i]),
i == n ? "\n" : "|"

Randal L. Schwartz

unread,
Nov 24, 2009, 11:19:54 AM11/24/09
to
>>>>> "Ben" == Ben Bacarisse <ben.u...@bsb.me.uk> writes:

Ben> map { s/ *$// } @f[4,5];

Please don't abuse map in a void context. (I know, I started
that with grep, and now I'm forever paying the karma penalty.)

Shorter and clearer to write:

s/ *$// for $f[4], $f[5];

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

Ben Bacarisse

unread,
Nov 24, 2009, 11:35:36 AM11/24/09
to
Loki Harfagr <l0...@thedarkdesign.free.fr.INVALID> writes:

> Mon, 23 Nov 2009 14:09:00 +0000, Ben Bacarisse did cat :

<snip>


>> Your post has had its lines broken so it is very hard to tell exactly
>> what it going on, but I think you'll need something like a Perl script
>> to do this.
>
> was that the reason why you broke the multipost and threw the cl.awk out?~>

'twas a cross-post surely?

It was mainly an error. I was going to post some Perl, but not
knowing comp.lang.awk's policy on such things I cut the group. But
then something came up and I had no time for code but did not restore
the groups.

--
Ben.

Ben Bacarisse

unread,
Nov 24, 2009, 11:58:44 AM11/24/09
to
mer...@stonehenge.com (Randal L. Schwartz) writes:

>>>>>> "Ben" == Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>
> Ben> map { s/ *$// } @f[4,5];
>
> Please don't abuse map in a void context. (I know, I started
> that with grep, and now I'm forever paying the karma penalty.)
>
> Shorter and clearer to write:
>
> s/ *$// for $f[4], $f[5];

[That's longer, but I know that is not really the point you are making.]

Is there any reason to avoid a slice here? If not, I'd go for:

s/ *$// for @f[4,5];

Can I just check I understand... Does the abuse come from the fact
that map builds the value even when it is to be thrown away, or is it
more a matter of style? I assume it is the waste.

--
Ben.

Ben Bacarisse

unread,
Nov 24, 2009, 12:46:35 PM11/24/09
to
mer...@stonehenge.com (Randal L. Schwartz) writes:

>>>>>> "Ben" == Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>
> Ben> map { s/ *$// } @f[4,5];
>
> Please don't abuse map in a void context. (I know, I started
> that with grep, and now I'm forever paying the karma penalty.)

Thinking this over some more, I see it not just wasteful. It sneakily
uses map for it's side-effect on referenced array elements.

--
Ben.

Ben Bacarisse

unread,
Nov 24, 2009, 12:51:31 PM11/24/09
to
Ed Morton <morto...@gmail.com> writes:
<snip>

> OK, for the purposes of winding up this comparison thread: given this
> input file (which I tweaked a bit from my previous post to include a
> case I hadn't thought of previously but I'm certain must show up in
> the OPs real input somewhere), output, and awk script:
>
> $ cat file
> Component Description Time Location
> --------- ----------- ---- --------
> SSC0 System Cont 05:21:53 Sriracha,
> roller GMT+05:00Chonburi,
> Thailand
>
> ID0 Sun Fire 48 02:35:54 Juarez Me
> 00 Centerpl GMT+05:00xico
> ane
>
> /N0/IB8 PCI I/O Big 08:42:03 Celestica
> Part GMT+05:00,Toronto,
> Ontario
>
> $ awk -f ed.awk file
> SSC0|System Controller|05:21:53 GMT+05:00|Sriracha,Chonburi,Thailand
> ID0|Sun Fire 4800 Centerplane|02:35:54 GMT+05:00|Juarez Mexico
> /N0/IB8|PCI I/O Big Part|08:42:03 GMT+05:00|Celestica,Toronto,Ontario
> $ cat ed.awk
> BEGIN { FIELDWIDTHS = "9 1 11 1 9 9"; skipFlds="^(2|4)$" }

I see why you were thinking "natural" -- GNU awk's FIELDWIDTHS helps
quite a bit here.

> NR>2 {
> if (NF) {
> numFlds=0
> for (i = 1; i <= NF; i++) {
> if (i !~ skipFlds) {
> numFlds++
> fld[numFlds] = fld[numFlds] $i
> }
> }
> } else {
> rec=sep=""
> for (i = 1; i <= numFlds; i++) {
> sub(/[[:space:]]+$/,"",fld[i])
> rec = rec sep fld[i]
> sep = "|"
> }
> print rec
> delete fld
> }
> }
>
> feel free to post the modified version of your perl script to use the
> adjusted field numbers/lengths if you like.

You've simplified the problem sightly so the code does get simpler:

$_ = <>; $_ = <>;
while (<>) {
if (/^$/) {
s/ *$// for @r;
print join('|', @r), "\n";
@r = ();
} else {
my @f = /(.{9}) (.{1,11}) ?(.{0,9})(.{0,9})/;
$r[$_] .= $f[$_] for 0..@f-1;
}
}

I should probably use better names for @f and @r like you have done.

--
Ben.

Ed Morton

unread,
Nov 24, 2009, 3:14:54 PM11/24/09
to
On Nov 24, 11:51 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:

Well, that does quite a lot in few lines and I really like the line:

my @f = /(.{9}) (.{1,11}) ?(.{0,9})(.{0,9})/;

which appears to be allowing you to specify the format of a string and
have an array populated from it's elements. Awk could certainly use
that rather than only having the more limitted FIELDWIDTSHs working on
records. I do think I'd prefer to see some of the brevity sacrificed
for a bit more clarity though - I guess you must get used to it over
time, but I find the perl syntax very hard to follow while from day
one I've always found the awk syntax generally pretty clear and
obvious. Maybe it depends what programming languages/paradigms you're
used to...

Ed.

Randal L. Schwartz

unread,
Nov 24, 2009, 5:28:40 PM11/24/09
to
>>>>> "Ben" == Ben Bacarisse <ben.u...@bsb.me.uk> writes:

Ben> mer...@stonehenge.com (Randal L. Schwartz) writes:
>>>>>>> "Ben" == Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>>
Ben> map { s/ *$// } @f[4,5];
>>
>> Please don't abuse map in a void context. (I know, I started
>> that with grep, and now I'm forever paying the karma penalty.)

Ben> Thinking this over some more, I see it not just wasteful. It sneakily
Ben> uses map for it's side-effect on referenced array elements.

Exactly.

First, most people don't know map/grep can *alter* $_. They
read it as a local safe-to-change variable.

Second, the "foreach" loop is shorter to type, less noisy to look
at, and doesn't build an output list.

True, modern Perl now detects map/grep-in-void-context and doesn't build the
list. But that's just because there were so many abuses. :)

print "Just another Perl hacker,"; # the original

Kevin Collins

unread,
Nov 24, 2009, 6:29:47 PM11/24/09
to
On 2009-11-24, Randal L. Schwartz <mer...@stonehenge.com> wrote:
>>>>>> "Ben" == Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>
>Ben> mer...@stonehenge.com (Randal L. Schwartz) writes:
>>>>>>>> "Ben" == Ben Bacarisse <ben.u...@bsb.me.uk> writes:
>>>
>Ben> map { s/ *$// } @f[4,5];
>>>
>>> Please don't abuse map in a void context. (I know, I started
>>> that with grep, and now I'm forever paying the karma penalty.)
>
>Ben> Thinking this over some more, I see it not just wasteful. It sneakily
>Ben> uses map for it's side-effect on referenced array elements.
>
> Exactly.
>
> First, most people don't know map/grep can *alter* $_. They
> read it as a local safe-to-change variable.

Too bad most people don't RTFM... both 'perldoc -f map' and 'perldoc -f grep'
explicitly state (as I'm sure you know!):

Note that $_ is an alias to the list value, so it can be used
to modify the elements of the LIST. While this is useful and

> Second, the "foreach" loop is shorter to type, less noisy to look
> at, and doesn't build an output list.
>
> True, modern Perl now detects map/grep-in-void-context and doesn't build the
> list. But that's just because there were so many abuses. :)

Kevin

Randal L. Schwartz

unread,
Nov 24, 2009, 9:11:19 PM11/24/09
to
>>>>> "Kevin" == Kevin Collins <spamt...@toomuchfiction.com> writes:

Kevin> Too bad most people don't RTFM... both 'perldoc -f map' and 'perldoc -f grep'
Kevin> explicitly state (as I'm sure you know!):

I know. I didn't just RTFM. I WTFM. :)

Unknown

unread,
Nov 25, 2009, 11:30:28 AM11/25/09
to
Ben Bacarisse wrote:

Ruby:

WIDTHS = 9,1,11,1,9,9
gets(); gets()
matrix = []
while gets()
if $_.strip.empty?
puts matrix.transpose.map{|x| x.join.strip }.join("|")
matrix = []
else
matrix << WIDTHS.map{|w| $_.slice!(0,w) }.values_at(0,2,4,5)
end
end

--

Kevin Collins

unread,
Nov 25, 2009, 12:59:59 PM11/25/09
to
On 2009-11-25, Randal L. Schwartz <mer...@stonehenge.com> wrote:
>>>>>> "Kevin" == Kevin Collins <spamt...@toomuchfiction.com> writes:
>
>Kevin> Too bad most people don't RTFM... both 'perldoc -f map' and 'perldoc -f grep'
>Kevin> explicitly state (as I'm sure you know!):
>
> I know. I didn't just RTFM. I WTFM. :)

Nice - I like that!

Kevin

0 new messages