4
\$\begingroup\$

I want to convert my_long_variable to myLongVariable in sed.

This works:

echo "my_long_variable" | sed -r 's/(^|_)([a-z])/\U\2/g' | sed -r 's/^(.)/\l\1/g'

Is there a more elegant way to do that with sed?

\$\endgroup\$
2
\$\begingroup\$

Why try to match for (^|_)? You are anyway modifying it at the end, so just skip the first character entirely:

echo "my_long_variable" | sed -r 's/_([a-z])/\U\1/gi' | sed -r 's/^([A-Z])/\l\1/'

The above pattern will take care of cases where you start with:

Some_var_Iable
\$\endgroup\$
1
\$\begingroup\$

First of all, prefer -E to -r; it's more portable.

However, since you're using the \U GNU extension in any case, this isn't that crucial.

Second, I assume echo is a stand-in for a more complex command. If there is in fact no complex command, consider using a Here String or a Here Doc instead.

Crucially, you don't need to fire up two Sed processes just to run two Sed commands. Sed is a complete programming language in itself. Just separate the two commands with a semicolon: sed 's/foo/bar/;s/frip/baz/'

However, in this case you don't even need that, because you only need a single substitution.

sed -E 's/_([a-z])/\U\1/g' <<< my_long_variable

There is another aspect here. You shouldn't often need to change case in scripts (and certainly not in Bash). But if you're doing this in your editor, you should use vi rather than a Bash script for the editing. \U and its relatives are standard features of vi, though not of Sed.

So within vi, on a line of text containing only the variable name, you can use:

:s/_\([a-z]\)/\U\1/g

This is portable (POSIX compliant) and will work on any system that has vi, even the most minimal implementation.

\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks! This is part of a code generation tool (with different modules using different languages with different conventions), so vi won't do, it needs to be automated. \$\endgroup\$ – Nicolas Raoul Nov 18 '16 at 4:49
  • \$\begingroup\$ @NicolasRaoul, gotcha! For scripted text edits, you may want to learn ex (the predecessor of vi), which is designed for editing text files (whereas Sed is the stream editor)—and is fully portable. I've written extensively on its use on the Unix & Linux Stack Exchange. \$\endgroup\$ – Wildcard Nov 18 '16 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.