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