coding question

Discussion in 'Perl Misc' started by rvaedex23@gmail.com, May 25, 2012.

  1. Guest

    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.
     
    , May 25, 2012
    #1
    1. Advertising

  2. Guest

    On May 25, 2:08 pm, 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 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
     
    , May 25, 2012
    #2
    1. Advertising

  3. Guest

    On Friday, May 25, 2012 4:08:18 PM UTC-4, wrote:
    > 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.

    On Friday, May 25, 2012 4:08:18 PM UTC-4, wrote:
    > 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.
     
    , May 27, 2012
    #3
  4. my $Parser = qr|^(..)(....)(...)(.*?)\s*$|;

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

    __DATA__
    aaBBBB123qwert
    bbCCCC456gerte
    ccDDDD789wwdfe
     
    George Mpouras, May 28, 2012
    #4
  5. Guest

    On Friday, May 25, 2012 4:08:18 PM UTC-4, wrote:
    > 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.....
     
    , May 29, 2012
    #5
  6. what is the question ?

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

    On Friday, May 25, 2012 4:08:18 PM UTC-4, wrote:
    > 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.....
     
    George Mpouras, May 29, 2012
    #6
  7. Guest

    On Friday, May 25, 2012 4:08:18 PM UTC-4, wrote:
    > 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.
     
    , May 30, 2012
    #7
  8. !!!
     
    George Mpouras, May 30, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. smu
    Replies:
    3
    Views:
    2,639
    shobhit24
    Jun 28, 2006
  2. Gregor

    Coding question

    Gregor, Feb 12, 2005, in forum: VHDL
    Replies:
    4
    Views:
    528
    Hal Murray
    Feb 13, 2005
  3. THY
    Replies:
    9
    Views:
    291
    John Saunders
    Aug 24, 2003
  4. CraigC
    Replies:
    8
    Views:
    488
  5. calmar
    Replies:
    11
    Views:
    901
    calmar
    Feb 21, 2006
Loading...

Share This Page