Domanda

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.

È stato utile?

Soluzione

An awk method for you:

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

Altri suggerimenti

$ 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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top