Adding a delimiter inbetween number characters and letter characters

Discussion in 'Perl Misc' started by toomanyjoes@mail.utexas.edu, Jan 12, 2005.

  1. Guest

    Hi,

    I just posted a message on deleting newline characters in perl.


    I used this code to do that:
    perl -pe "chomp;print $/ if $.>1 and /^\d/" file1 > file2

    I ended up with a new text file in this format:

    1sampletextsampletextsamplet22tsampletextsampletextsampletext
    2sampletextsampletextsampletextsam56etextsampletextsampletext
    3sampletextsampletextsampletextsampletextsampletextsampletext
    8sampletextsampletextsampletex765mpletextsampletextsampletext
    9sampletextsampletextsampletextsampletextsampletextsampletext
    15sampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsample324tsampletextsampletext
    ....

    Now what I want is to add a delimiter inbetween the number at the
    beginning of the line and the text. The number however could be any
    number of digits long. So for instance if I use a "/" as a delimiter
    the text would look like this.

    1/sampletextsampletextsamplet22tsampletextsampletextsampletext
    2/sampletextsampletextsampletextsam56etextsampletextsampletext
    3/sampletextsampletextsampletextsampletextsampletextsampletext
    8/sampletextsampletextsampletex765mpletextsampletextsampletext
    9/sampletextsampletextsampletextsampletextsampletextsampletext
    15/sampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsample324tsampletextsampletext

    This way I can go from a text file to Excel and then into a database.
    Could anyone help me with this?

    Thanks,
    Joe
     
    , Jan 12, 2005
    #1
    1. Advertising

  2. Paul Lalli Guest

    <> wrote in message
    news:...
    > I ended up with a new text file in this format:
    >
    > 1sampletextsampletextsamplet22tsampletextsampletextsampletext
    > 2sampletextsampletextsampletextsam56etextsampletextsampletext
    > 3sampletextsampletextsampletextsampletextsampletextsampletext
    > 8sampletextsampletextsampletex765mpletextsampletextsampletext
    > 9sampletextsampletextsampletextsampletextsampletextsampletext
    >

    15sampletextsampletextsampletextsampletextsampletextsampletextsampletext
    sampletextsampletextsample324tsampletextsampletext
    > ...
    >
    > Now what I want is to add a delimiter inbetween the number at the
    > beginning of the line and the text. The number however could be any
    > number of digits long. So for instance if I use a "/" as a delimiter
    > the text would look like this.
    >
    > 1/sampletextsampletextsamplet22tsampletextsampletextsampletext
    > 2/sampletextsampletextsampletextsam56etextsampletextsampletext
    > 3/sampletextsampletextsampletextsampletextsampletextsampletext
    > 8/sampletextsampletextsampletex765mpletextsampletextsampletext
    > 9/sampletextsampletextsampletextsampletextsampletextsampletext
    >

    15/sampletextsampletextsampletextsampletextsampletextsampletextsampletex
    tsampletextsampletextsample324tsampletextsampletext
    >
    > This way I can go from a text file to Excel and then into a database.
    > Could anyone help me with this?


    This is a trivial operation using regular expressions and
    search-and-replace. I suggest you read up:

    perldoc perlretut
    perldoc perlrequick
    perldoc perlre

    Paul Lalli
     
    Paul Lalli, Jan 12, 2005
    #2
    1. Advertising

  3. Guest

    Anyone can feel free to post helpful messages despite Paul Lalli's
    post. I realize it is trivial, I'm not a Perl programmer, and I am
    reading up, but my project is waiting for this one simple task to be
    completed before moving on so the faster I figure this out the better.
    Paul if its so easy why not code to do it? I suggest you read up on
    newsgroup etiquette.
     
    , Jan 12, 2005
    #3
  4. Guest

    Anyone can feel free to post helpful messages despite Paul Lalli's
    post. I realize it is trivial, I'm not a Perl programmer, and I am
    reading up, but my project is waiting for this one simple task to be
    completed before moving on so the faster I figure this out the better.
    Paul if its so easy why not post code to do it? I suggest YOU read up
    on newsgroup etiquette.
     
    , Jan 12, 2005
    #4
  5. Scott Bryce Guest

    wrote:

    > Anyone can feel free to post helpful messages despite Paul Lalli's
    > post. I realize it is trivial, I'm not a Perl programmer, and I am
    > reading up, but my project is waiting for this one simple task to be
    > completed before moving on so the faster I figure this out the better.
    > Paul if its so easy why not post code to do it? I suggest YOU read up
    > on newsgroup etiquette.


    Paul gave you good advice. You can learn more from the docs than you can
    learn from blindly copying code. Paul showed you exactly where to read
    in the docs.

    Perhaps you should read the posting guidelines for this group. Now that
    you have insulted Paul, you are probably on your own.
     
    Scott Bryce, Jan 12, 2005
    #5
  6. Paul Lalli Guest

    <> wrote in message
    news:...
    > Anyone can feel free to post helpful messages despite Paul Lalli's
    > post.


    My post was extremely helpful.

    > I realize it is trivial, I'm not a Perl programmer, and I am
    > reading up, but my project is waiting for this one simple task to be
    > completed before moving on


    The failure of your project coordinator to hire correct staff is not
    justification for asking this group to do your project's work for you.

    > so the faster I figure this out the better.


    The better for you, perhaps. The better for the rest of the people
    reading this group, not so much, no.

    > Paul if its so easy why not post code to do it?


    Because I *know* how to do it. I am not the one who needs to learn.

    > I suggest YOU read up on newsgroup etiquette.


    Really? Would that etiquette include such things as posting the same
    message twice? Would it include not reading the posting guidelines for
    the group? Would it include not quoting the material your are
    responding to?

    Please do go read those guidelines. Then try to understand that this is
    not a group where it is acceptable to ask "Someone please do this for
    me." This is a group where beginners are encouraged to ask for help or
    understanding when it comes to *learning* the Perl language.

    Paul Lalli
     
    Paul Lalli, Jan 12, 2005
    #6
  7. Guest

    In spite of Scotts message anyone can feel free to post useful
    information. Scott, I already knew about those references. I've been
    going through them. My post did not violate any rules for this group, I
    have read those too. I am not new to newgroups, only to the Perl
    language. You guys could make someone new here a little more
    comfortable posting questions. Rather than more people telling me how
    little I know about Perl, I would be much oblidged to anyone who can
    post a little snippet of code for me.
     
    , Jan 12, 2005
    #7
  8. Guest

    In spite of Scotts message anyone can feel free to post useful
    information. Scott, I already knew about those references. I've been
    going through them. My post did not violate any rules for this group, I
    have read those too. I am not new to newgroups, only to the Perl
    language. You guys could make someone new here a little more
    comfortable posting questions. Rather than more people telling me how
    little I know about Perl, I would be much oblidged to anyone who can
    post a little snippet of code for me.
     
    , Jan 12, 2005
    #8
  9. Guest

    I'm not going to let my temper get the better of me Paul, like you just
    did. Showing the passion of your emotions is very unattractive in
    groups like this. Perhaps my "project coordinator" hired the wrong
    "staff", true. But the facts are:

    1) I have to get this done
    2) Your post was of no help to me
    3) I still need help

    I hope you will accept an apology for insulting you. I have read the
    guidelines for this group. I've been posting on these groups for over
    10 years back when it was DejaNews and it has and probably will remain
    a forum to ask people to write code for you, many people learn this way.
     
    , Jan 12, 2005
    #9
  10. wrote:

    > In spite of Scotts message anyone can feel free to post useful
    > information.


    You can keep parroting that cute little catch-phrase of yours all you want -
    the fact remains that the people you're berating *did* post useful
    information.

    Frankly, given your attitude, I'd imagine it's the last useful information
    you'll get from this group.

    > You guys could make someone new here a little more
    > comfortable posting questions.


    Making people comfortable posting questions not our goal here. Our goal is
    to make people comfortable with the docs, so they don't have to post
    questions.

    To put it another way, if you've come looking for free fish, you're in the
    wrong place. We give fishing lessons here.

    > I would be much oblidged to anyone who can
    > post a little snippet of code for me.


    If you want code written to spec, email me privately for my consulting
    rates.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jan 12, 2005
    #10
  11. Scott Bryce Guest

    wrote:

    > My post did not violate any rules for this group, I have read those
    > too.


    But the posting guidelines said:

    > First make a short (less than 20-30 lines) and *complete* program
    > that illustrates the problem you are having. People should be able to
    > run your program by copy/pasting the code from your article.
     
    Scott Bryce, Jan 12, 2005
    #11
  12. wrote:

    > I ended up with a new text file in this format:
    >
    > 1sampletextsampletextsamplet22tsampletextsampletextsampletext
    > 2sampletextsampletextsampletextsam56etextsampletextsampletext
    > 3sampletextsampletextsampletextsampletextsampletextsampletext
    > 8sampletextsampletextsampletex765mpletextsampletextsampletext
    > 9sampletextsampletextsampletextsampletextsampletextsampletext
    > 15sampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsample324tsampletextsampletext
    > ...
    >
    > Now what I want is to add a delimiter inbetween the number at the
    > beginning of the line and the text. The number however could be any
    > number of digits long. So for instance if I use a "/" as a delimiter
    > the text would look like this.
    >
    > 1/sampletextsampletextsamplet22tsampletextsampletextsampletext
    > 2/sampletextsampletextsampletextsam56etextsampletextsampletext
    > 3/sampletextsampletextsampletextsampletextsampletextsampletext
    > 8/sampletextsampletextsampletex765mpletextsampletextsampletext
    > 9/sampletextsampletextsampletextsampletextsampletextsampletext
    > 15/sampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsampletextsample324tsampletextsampletext


    perl -pe 's/^(\d+)/$1\//'

    > Could anyone help me with this?


    Note I have helped you despite and not because of your complaints at
    being told to RTFM.
     
    Brian McCauley, Jan 12, 2005
    #12
  13. Paul Lalli Guest

    <> wrote in message
    news:...
    > I'm not going to let my temper get the better of me Paul, like you

    just
    > did.


    You continue to not following the posting guidelines, so I don't know
    exactly what you're referring to. I'm not aware of exhibiting any
    temper in my posts.

    > Showing the passion of your emotions is very unattractive in
    > groups like this. Perhaps my "project coordinator" hired the wrong
    > "staff", true. But the facts are:
    >
    > 1) I have to get this done


    Which remains not our problem.

    > 2) Your post was of no help to me


    Of course it was. It just wasn't the help you wanted.

    > 3) I still need help


    You've been given help. You choose not to accept it.

    > I hope you will accept an apology for insulting you.


    You can't offern an apology for insulting me in the same message in
    which you continue to insult me. It doesn't work that way. Thanks for
    the attempt, however.

    > I have read the guidelines for this group.


    And yet, have chosen not to follow them. That's actually worse than not
    reading them, in my mind.

    > I've been posting on these groups for over
    > 10 years back when it was DejaNews


    I don't know which groups you're referring to, but they don't include
    this one.

    > and it has and probably will remain
    > a forum to ask people to write code for you, many people learn this

    way.

    No. It isn't, and it won't. Please search the archives of this group.
    See how well previous requests for "write this for me" have been
    received.

    If you want code written for you, I'd be happy to help. Feel free to
    respond privately and we can discuss my professional rates.

    If you want to learn, I have already given you pointers on where to
    start. I'd be happy to help you understand that documentation, or to
    help you correct any attempts you've made, free of charge.

    Paul Lalli
     
    Paul Lalli, Jan 12, 2005
    #13
  14. Scott Bryce Guest

    wrote:

    > I have read the guidelines for this group.


    But you don't follow them.


    > I've been posting on these groups for over
    > 10 years back when it was DejaNews


    These groups have NEVER been DejaNews. DejaNews was an archive of these
    groups.

    > and it has and probably will remain
    > a forum to ask people to write code for you


    It never has been.
     
    Scott Bryce, Jan 12, 2005
    #14
  15. wrote:

    > I'm not going to let my temper get the better of me Paul


    Too late.

    > Showing the passion of your emotions is very unattractive


    Yes it is - why are you doing it?

    > 2) Your post was of no help to me


    Paul told you precisely where to find the answers you need? In what way was
    that unhelpful? What did you expect, ready-to-run code written to spec?

    > I've been posting on these groups for over
    > 10 years back when it was DejaNews


    Silly rabbit. This isn't "Google Groups", and it wasn't "DejaNews" - it's
    usenet, and those are nothing but web-based front ends for it. Since you're
    so fond of Google, try searching for "usenet history" and studying up a bit
    before you mouth off.

    > and it has and probably will remain
    > a forum to ask people to write code for you


    No, that is completely and utterly wrong. This is a forum for people who
    want to learn to write their own code. If you want to hire someone to write
    code for you, try a group with "jobs" in its name.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jan 12, 2005
    #15
  16. Guest

    Ahhhh...Thank you guys. I apologize for this threads explosion, it got
    a little out of hand. I just in practice never reply to a post unless I
    have the answer so it irks me when others don't follow that practice.
    Next time I'll just remain silent. I really thank you guys for your
    help.
     
    , Jan 12, 2005
    #16
  17. wrote:

    > I just in practice never reply to a post unless I
    > have the answer so it irks me when others don't follow that practice.


    Paul *did* follow that practice. He told you where you could find the answer
    to your question. Quit whining.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jan 12, 2005
    #17
  18. Guest

    One last post Sherm, then I'm done. No matter what obscenities you
    respond with, and understand this, I'm happy. The code snippet Jim and
    Brian gave me worked. Though in the spirit of learning I wouldn't mind
    getting a break down of what each portion does, RegEx's confuse me.

    But heres what I want to say, and in all logic try to understand,
    putting emotions aside. When you ask someone how to do something and
    they say "read the manual" in practice that is basically dismissing the
    question. It has an arrogant "better than thou" tone to it. Is this
    hard to understand? Perhaps Paul didn't understand this and still
    doesn't recognize it. Perhaps he didn't even mean it this way, but
    anyone who has asked a question and gotten "Pssh, thats easy read the
    manual" knows exactly what I mean. First that assumes I haven't read
    those docs. Which I am still doing (and was doing before I asked the
    question) because I have a few more things I need to do in Perl after
    this. But I don't see the harm in posting a question before I know
    everything about Perl. Now I realize it is a mortal sin to have any
    kind of response to a person on newsgroups who basically tells you to
    "**** off" without multiple allies otherwise you get a barrage of
    emails from that person and his cronies defending him. Thats a mistake
    I made. I'm lucky that some people still were willing to help me
    because no matter what you say after you have a swarm of enemies
    bombarding you with replies, you always come out the bad guy. So thanks
    to those who helped. But I just want to make that clear.
     
    , Jan 12, 2005
    #18
  19. wrote :
    > In spite of Scotts message anyone can feel free to post useful
    > information. Scott, I already knew about those references. I've been
    > going through them. My post did not violate any rules for this group, I
    > have read those too. I am not new to newgroups, only to the Perl
    > language. You guys could make someone new here a little more
    > comfortable posting questions. Rather than more people telling me how
    > little I know about Perl, I would be much oblidged to anyone who can
    > post a little snippet of code for me.
    >

    I guess the problem is not, that you know little about perl.
    The problem might rather be, that you don't know how to ask the right
    questions.
    Maybe this page can help you understand:
    http://www.catb.org/~esr/faqs/smart-questions.html

    However, here is some code for you.
    Since I'm new to Perl, too, this is certanly not the best way to solve
    your problem, but it works.
    I took your OP, to analyze the problem by myself and practice coding.

    1 #!/usr/bin/perl
    2
    3 use warnings;
    4 use strict;
    5 my $lastline="";
    6 my @lastline;
    7
    8 while (<DATA>)
    9 {
    10 chomp ($lastline) if (/^\D/);
    11 &printline;
    12 $lastline = $_;
    13 }
    14 &printline;
    15
    16 sub printline
    17 {
    18 if ($lastline =~ /^\d+/)
    19 {
    20 @lastline = split /(^\d+)/, $lastline;
    21 print "$lastline[1]\t<-->\t";
    22 print "--$lastline[2]";
    23 }
    24 else
    25 {
    26 print $lastline;
    27 }
    28 }
    29
    30 __DATA__
    31 1sampletextsampletextsamplet22tsampletextsampletextsampletext
    32 2sampletextsampletextsampletextsam56etextsampletextsampletext
    33 3sampletextsampletextsampletextsampletextsampletextsampletext
    34 8sampletextsampletextsampletex765mpletextsampletextsampletext
    35 9sampletextsampletextsampletextsampletextsampletextsampletext
    36 15sampletextsampletextsampletextsampletextsampletextsampletext
    37 sampletextsampletextsampletextsample324tsampletextsampletext
    38 18sampletextsampletextsampletextsampletextsampletextsampletext
    39 sampletextsampletextsampletextsampletextsampletextsampletext
    40 sampletextsampletext566pletextsampletextsampletextsampletext
    41 22sampletextsampletextsampletextsampletextsampletextsampletext
    42 1sampletextsampletextsamplete467ampletextsampletextsampletext

    --
    Epur Si Muove (Gallileo Gallilei)
     
    Martin Kissner, Jan 12, 2005
    #19
  20. wrote:

    > One last post Sherm, then I'm done. No matter what obscenities you
    > respond with


    Obscenities? What are you talking about? *You* dropped the "F-Bomb" in your
    post - I haven't done anything of the sort.

    > The code snippet Jim and Brian gave me worked.


    Did you read their posts? They both commented on your bad attitude, and
    indicated that they're being charitable and helping you despite of it.

    > But heres what I want to say, and in all logic try to understand,
    > putting emotions aside.


    Um... what emotions? Seriously. You're throwing the tantrum here, not me.
    I'm trying to explain to you how your behavior will affect the quality and
    type of responses you get here. I'm trying to help you - despite your poor
    behavior.

    > When you ask someone how to do something and
    > they say "read the manual" in practice that is basically dismissing the
    > question.


    Well, it's a good thing Paul didn't say that, now isn't it? He didn't just
    dismiss you with "RTFM" - he gave you a nudge in the general direction by
    telling you what parts of the manual would be most useful to you.

    > It has an arrogant "better than thou" tone to it.


    Nonsense. I'm pointing you to the *same* docs I learned from. I'm confident
    that you can learn from them just as well as I did. If I were arrogant I
    wouldn't think that.

    > anyone who has asked a question and gotten "Pssh, thats easy read the
    > manual" knows exactly what I mean.


    That's not what you got. You got "That's easy with regexes - have a look at
    chapter X of the manual to learn about them." It might not have been the
    handout you were hoping for, but it *was* a useful push to help get you
    started in the right direction.

    > But I don't see the harm in posting a question before I know
    > everything about Perl.


    No one told you you needed to "know everything" before posting a question.
    What we're telling you is that you should make some attempt at solving the
    problem yourself before asking for help.

    There's a difference between asking for help and asking for a handout. You
    asked for a handout, and this group strongly discourages that.

    > Now I realize it is a mortal sin to have any
    > kind of response to a person on newsgroups who basically tells you to
    > "**** off" without multiple allies otherwise you get a barrage of
    > emails from that person and his cronies defending him. Thats a mistake
    > I made.


    Isn't that exactly the sort of emotional, obscenity-laced outburst you
    imagined I was guilty of?

    > I'm lucky that some people still were willing to help me


    Yes you were. Do you intend to depend on good luck in the future? If so, I
    hope it works out well for you.

    > because no matter what you say after you have a swarm of enemies
    > bombarding you with replies, you always come out the bad guy.


    It *does* matter what you say. That's the point of all this. If you had read
    the suggested docs and tried to write some code, then come back here and
    said "here's what I've tried so far - I expected it to do X, but it did Y
    instead" there would have been no problem. You'd have been up to your
    eyebrows in comments about your code, instead of comments about your
    attitude.

    Instead you complained that you didn't get the handout you expected.
    Naturally, several people - those you imagine as "enemies" - tried to
    explain to you that this isn't the place to come asking for handouts.

    > So thanks to those who helped.


    You're quite welcome.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jan 12, 2005
    #20
    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. Girish
    Replies:
    3
    Views:
    436
    Girish
    Feb 24, 2004
  2. Mike Miller
    Replies:
    1
    Views:
    400
    Murat G.
    Oct 19, 2003
  3. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    786
    Reinhold Birkenfeld
    Jul 6, 2004
  4. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    518
    Tony Meyer
    Jul 6, 2004
  5. jason

    difference inbetween macro & function

    jason, Nov 6, 2007, in forum: C Programming
    Replies:
    6
    Views:
    444
    Charlie Gordon
    Nov 8, 2007
Loading...

Share This Page