coding question

R

rvaedex23

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.
 
J

jl_post

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 andI 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
 
R

rvaedex23

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.

Thank you all for all your input. I will try this on Tuesday.
 
G

George Mpouras

my $Parser = qr|^(..)(....)(...)(.*?)\s*$|;

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

__DATA__
aaBBBB123qwert
bbCCCC456gerte
ccDDDD789wwdfe
 
R

rvaedex23

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.

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.....
 
G

George Mpouras

what is the question ?

Ο έγÏαψε στο μήνυμα

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.

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.....
 
R

rvaedex23

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.



Thanks, I was able to accomplish this using gawk.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top