質問

Purely just for fun, I was wondering how to transform all lower case text into upper case for every other letter, using either awk, sed, tr, regex or any other Unix tool except for Python or Perl.

So far I've tried

echo "hello" | sed s/\([a-z]\)/\U\1/g 

the desired output should be HeLlO. I would appreciate if anyone can improve this or offer an alternative approach.

役に立ちましたか?

解決

An awk method for you:

echo "hello" | awk '{for(i=1;i<=NF;i+=2)$i=toupper($i)}1' FS= OFS=
HeLlO

他のヒント

$ sed 's/[a-z].\?/\u\0/g' <<< hello
HeLlO

(Disclaimer: this may depend on non-standard features. I ran it on GNU sed version 4.2.1.)

This might work for you (GNU sed):

sed 's/[[:lower:]][^[:alpha:]]*\([[:alpha:]]\|$\)/\u&/g' file
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top