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

coding question

19 views
Skip to first unread message

rvae...@gmail.com

unread,
May 25, 2012, 4:08:18 PM5/25/12
to
I am not very familiar with perl. Although, I do have shell scripting experience.
I thought this would be easier to do this in perl.

I have a file that doesn't have a field delimiter between the columns and I want to add a colon between each column. I can identify the columns by hard coding the range:
such as "col1 to col 10" "col11 to col24" etc...

I am stuck. Any advise would help. Thanks.

jl_...@hotmail.com

unread,
May 25, 2012, 4:52:05 PM5/25/12
to

On May 25, 2:08 pm, rvaede...@gmail.com wrote:
> I thought this would be easier to do this in perl.
>
> I have a file that doesn't have a field delimiter between the columns and I want to add a colon between each column.  I can identify the columns by hard coding the range:
> such as "col1 to col 10" "col11 to col24" etc...
>
> I am stuck.  Any advise would help.  Thanks.


If the column ranges are guaranteed to be the same, you can do
something like:

my @fields = unpack("a10 a14", $line);

to extract out the fields in each column. Then you can re-print them
with colons in between them like this:

print join(':', @fields);


Here's a quick one-line solution. If you have a file (named
"input.txt") with these two lines:

1234567890abcdefghijklmn
happy day

Then running this one-liner:

# For Unix:
perl -wlne 'print join(":", unpack("a10 a14", $_))' input.txt
# For Windows/DOS:
perl -wlne "print join(':', unpack('a10 a14', $_))" input.txt

will get you this output:

1234567890:abcdefghijklmn
happy :day

Note that trailing spaces aren't stripped (you can see an example
of this this in the second line of output). If you want them to be
stripped, replace "a10 a14" with "A10 A14". Then your output will
look like:

1234567890:abcdefghijklmn
happy:day

You can look up the documentation for join() with "perldoc -f
join". You can read the documentation for unpack() with "perldoc -f
unpack". You can look up the letter templates (such as "a" and "A"
that you pass into unpack()) with "perldoc -f pack".

I hope this helps!

-- Jean-Luc

Ben Morrow

unread,
May 25, 2012, 7:19:16 PM5/25/12
to

Quoth rvae...@gmail.com:
What have you tried?

Something like

perl -ple's/(.{10})(.{14)/$1:$2/'

might be what you're looking for.

Ben

rvae...@gmail.com

unread,
May 27, 2012, 10:21:41 AM5/27/12
to
Thank you all for all your input. I will try this on Tuesday.

George Mpouras

unread,
May 28, 2012, 4:43:45 PM5/28/12
to
my $Parser = qr|^(..)(....)(...)(.*?)\s*$|;

while(<DATA>) {
@{$_} = $_ =~$Parser or next;
print join(',', @{$_}),"\n"
}

__DATA__
aaBBBB123qwert
bbCCCC456gerte
ccDDDD789wwdfe

rvae...@gmail.com

unread,
May 29, 2012, 9:25:57 AM5/29/12
to
On Friday, May 25, 2012 4:08:18 PM UTC-4, rvae...@gmail.com wrote:
One more question:

I found out that I have to check two bytes in the file and
if the byte = 14
then
I need to attach the colon after the field 16, 20, 36, 58
if the bye = 15
then
I need to attach the colon after the field 24, 37, 70, 88
etc.....

George Mpouras

unread,
May 29, 2012, 5:39:00 PM5/29/12
to
what is the question ?

Ο έγραψε στο μήνυμα
news:c1d3f454-3db2-4bb0...@googlegroups.com...

rvae...@gmail.com

unread,
May 30, 2012, 9:24:36 AM5/30/12
to
On Friday, May 25, 2012 4:08:18 PM UTC-4, rvae...@gmail.com wrote:
Thanks, I was able to accomplish this using gawk.

George Mpouras

unread,
May 30, 2012, 1:17:20 PM5/30/12
to
!!!
0 new messages