Reputation: 3151
This picks all the text on single line after a pattern match, and converts it to camel case using non-alphanumeric as separator, remove the spaces at the beginning and at the end of the resulting string, (1) this don't replace if it has 2 consecutive non-alphanumeric chars, e.g "2, " in the below example, (2) is there a way to do everything using sed command instead of using grep, cut, sed and tr
?
$ echo " hello
world
title: this is-the_test string with number 2, to-test CAMEL String
end! " | grep -o 'title:.*' | cut -f2 -d: | sed -r 's/([^[:alnum:]])([0-9a-zA-Z])/\U\2/g' | tr -d ' '
ThisIsTheTestStringWithNumber2,ToTestCAMELString
Upvotes: 1
Views: 683
Reputation: 626691
To answer your first question, change [^[:alnum:]]
to [^[:alnum:]]+
to mach one ore more non-alnum chars.
You may combine all the commands into a GNU sed
solution like
sed -En '/.*title: *(.*[[:alnum:]]).*/{s//\1/;s/([^[:alnum:]]+|^)([0-9a-zA-Z])/\U\2/gp}'
See the online demo
Details
-En
- POSIX ERE syntax is on (E
) and default line output supressed with n
/.*title: *(.*[[:alnum:]]).*/
- matches a line having title:
capturing all after it up to the last alnum char into Group 1 and matching the rest of the line{s//\1/;s/([^[:alnum:]]+|^)([0-9a-zA-Z])/\U\2/gp}
- if the line is matched,
s//\1/
- remove all but Group 1 pattern (received above)s/([^[:alnum:]]+|^)([0-9a-zA-Z])/\U\2/
- match and capture start of string or 1+ non-alnum chars into Group 1 (with ([^[:alnum:]]+|^)
) and then capture an alnum char into Group 2 (with ([0-9a-zA-Z])
) and replace with uppercased Group 2 contents (with \U\2
).Upvotes: 1