Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Editing File In-Place to put tab before UpperCase Letters

by ranjan_jajodia (Monk)
on Jul 23, 2005 at 06:11 UTC ( [id://477430]=perlquestion: print w/replies, xml ) Need Help??

ranjan_jajodia has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I have a file whose entries are in the following format:

JohnGalt
DagnyTagart
HenryHankRearden
...

I want the content to be in the following format:

John Galt
Dagny Tagart
Henry Hank Rearden
...

i.e, putting tabs(\t) or spaces(\b) before every Upper case letter. Is there a easy way out like doing it by in-place editing. I think s or tr cannot be used here and i need some way to insert character.
Could any one point me in the right direction? If it is not possible than i would have to write a complete script to do that which i feel is not needed here.

Ranjan
--Let Logic Rule--
  • Comment on Editing File In-Place to put tab before UpperCase Letters

Replies are listed 'Best First'.
Re: Editing File In-Place to put tab before UpperCase Letters
by Zaxo (Archbishop) on Jul 23, 2005 at 06:19 UTC

    Let's do it in unicode, perl -pi -e's/(\p{IsUpper})/\t$1/g' text.txt If you have some way of determining whether you want spaces or tabs, you should say what it is.

    After Compline,
    Zaxo

      Thanks Zaxo for the solution. Sorry for the ambiguity regarding whether I wanted tab or space.

      Ranjan
      --Let Logic Rule--
Re: Editing File In-Place to put tab before UpperCase Letters
by Your Mother (Archbishop) on Jul 23, 2005 at 06:23 UTC

    Slight mod to avoid leading space and need for capture. Puts a space (" ") instead of a tab. "\b" is not a space but a boundary.

    perl -pi -e 's/(?!\A)(?=\p{IsUpper})/ /g'
      Putting "\b" in the string would add a backspace, not a word boundary.

        Er, quite right, of course. I was thinking of the left side. Pattern match--boundary, interpolated string--backspace.

      Thanks for the solution and additional information. It works perfectly.

      Ranjan
      --Let Logic Rule--

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://477430]
Approved by Zaxo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-18 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found