Split string on capital letter, not on multiple capital letters
For a shell script used to automatically generate c++ code files, I have to split certain names apart for them to be formatted. I split them at a capital letter, but I do not want to split if there are multiple capital letters in sequence. For example:
I want this (and I get this):
ChanNameWordWrapMode -> Chan Name Word Wrap Mode
I want this:
ChanNameVAlign -> Chan Name VAlign
OSDSettings -> OSD Settings
But I get this:
ChanNameVAlign -> Chan Name V Align
OSDSettings -> O S D Settings.
The command I am using right now is:
`echo $string | sed 's/\([A-Z]\)/ \1/g' | sed 's/^ *//;s/ *$//'`
Can someone help with a sed command that does what I am asking? Thanks.
11 Months Ago
Last Updated
Related Article: Replace String in a C Shell Script
is a solved Shell Scripting discussion thread by msrikanth that has 9 replies, was last updated 1 year ago and has been tagged with the keywords: awk, script, sed, string, unix.
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
In the ChanNameVAlign case you want to split at the transition from lowercase to uppercase:
s/\([a-z]\)\([A-Z]\)/\1 \2/g
In the OSDSettings, I don't see a non-contradictory criteria.
nezachem
Posting Shark
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 0
Thanks. I will try that, but the current regex does split OSDSettings into O S D Settings
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Okay, I thought of something to try, but I can't seem to construct the regex. If I have a string of the format "O S D Settings" that I want to convert to "OSD Settings", I could just concatenate the capital letters that are followed by a space, right? How might I construct this Regex?
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
This is working part way:
sed 's/ \([A-Z]\) \([A-Za-z]*\)/\1/g'
However, when run on the string "O S D Settings", it returns "OS Settings" - it is only operating on the first instance of the regex. Any tips on how to make it work for the whole line?
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
concatenate the capital letters that are followed by a space
I can think only of a 3-step solution (which looks ugly, but possibly it can be improved):
- replace all wanted spaces with, say, underscores:
s/ \([A-Z][^ ]\)/_\1/g'
- remove all remaining spaces:
s/ //g
- restore wanted spaces:
s/_/ /g'
Something like that...
nezachem
Posting Shark
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 0
Well, you don't have to use three commands - you can use pipes to redirect the code:
echo $string | sed 's/ \([A-Z][^ ]\)/_\1/g' | sed 's/ //g' | sed 's/_/ /g'
Anyway, thanks. This works. Thanks for all the help.
iamthesgt
Junior Poster
119 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 11 Months Ago by
nezachem