Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Editing File In-Place to put tab before UpperCase Letters

by ranjan_jajodia (Monk)
on Jul 23, 2005 at 06:11 UTC ( #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:


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.

--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,

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

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

      --Let Logic Rule--

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2023-10-04 04:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found