문제

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