Linux Shell Release Statement

I wring the shell script and have this variable: something-that-is-hyphenated .

I need to use it at different points in the script like:

something-that-is-hyphenated , somethingthatishyphenated , somethingthatishyphenated

I managed to change it to somethingthatishyphenated by deleting - using sed "s/-//g" .

I am sure there is an easier way, and you also need to know how to get the camel shell version.

Edit: working function derived from @ Michał's answer

 function hyphenToCamel { tr '-' '\n' | awk '{printf "%s%s", toupper(substr($0,1,1)), substr($0,2)}' } CAMEL=$(echo something-that-is-hyphenated | hyphenToCamel) echo $CAMEL 

Editing Finally, sed one liner thanks to @glenn

 echo a-hyphenated-string | sed -E "s/(^|-)([az])/\u\2/g" 
+6
source share
5 answers

a single line GNU sed

 echo something-that-is-hyphenated | sed -e 's/-\([az]\)/\u\1/g' -e 's/^[az]/\u&/' 

\u in the replacement string is documented in the sed manual .

+4
source

Pure Bagism:

 var0=something-that-is-hyphenated var1=(${var0//-/ }) var2=${var1[*]^} var3=${var2// /} echo $var3 SomethingThatIsHyphenated 

Line 1 is trivial. Line 2 is the bashism for replaceAll or 's/-/ /g' wrapped in parens to build an array.
Line 3 uses ${foo^} , which means an uppercase letter (while ${foo,} means "lower case" [note, as ^ points up, but , points down]), but to work with each first with the letter of the word we are accessing the entire array with ${foo[*]} (or ${foo[@]} if you prefer this).
Line 4 is again a replacement: everything is empty.
Line 5 is trivial again.

+4
source

You can define a function:

 hypenToCamel() { tr '-' '\n' | awk '{printf "%s%s", toupper(substr($0,0,1)), substr($0,2)}' } CAMEL=$(echo something-that-is-hyphenated | hypenToCamel) echo $CAMEL 
+1
source

In the shell, you are stuck in desolation:

  aa = "aaa-aaa-bbb-bbb"
    echo "$ aa" |  sed -e 's / - * / / g' -e 's / a / A / g' -e 's / b / B / g' ... -e 's / * // g'

Pay attention to the carefully placed space in the echo and the double space in the last -th.

I leave this as an exercise to complete the code.

In perl, this is a little simpler than a single-line shell command:

  perl -e 'print map {$ a = ucfirst;  $ a = ~ s / + // g;  $ a} split (/ - + /, $ ARGV [0]), "\ n" '$ aa
0
source

For entries here, a pure Bash safe method is used (which cannot be expanded with a path name) - using Bash ≥4:

 var0=something-that-is-hyphenated IFS=- read -r -d '' -a var1 < <(printf '%s\0' "${var0,,}") printf '%s' "${var1[@]^}" 

This (safely) splits the bottom var0 extension into hyphens with each split part in the var1 array. Then we use the extension of the ^ parameter to smooth the first character of the fields of this array and combine them.

If your variable may also contain spaces, and you must also act on them, change IFS=- to IFS='- ' .

0
source

Source: https://habr.com/ru/post/894454/


All Articles