Pro Coder
Pro Coder

Reputation: 71

Uppercase only the first letter of each word and rest lower

My file is

 cat bla.txt 
AbcDe xYaG

need Abcde Xyzg

I am using sed Didnt work

cat bla.txt | sed "s/.*/\L&/g" | sed "s/\w/\u&/"
LAbcDe xYaG

Thanks it work

perl -pe 's/\w+/\L\u$&/g'

Upvotes: 2

Views: 204

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185254

Using , the simplest and readable solution ;)

perl -lne 'print ucfirst for split /\b/, lc' file

or :

perl -pe 's/\w+/\L\u$&/g'

Credits to Sundeep for this one

 Output

Abcde Xyag

 Explanations

print()      # do what you think
ucfirst()    # UPPER case the first letter
for()        # perform preceding calls for each element of following list
split(/\b/)  # split string on word boundaries
lc()         # lower-case each element from splited list

Upvotes: 2

Sundeep
Sundeep

Reputation: 23667

$ echo 'AbcDe xYaG' | sed -E 's/\w+/\L\u&/g'
Abcde Xyag
  • -E to enable ERE
  • \w+ match one or more word characters, since longest match wins, entire words will be matched
  • \L\u& here \L will cause characters to be lowercased, but \u will override it and cause first character to be uppercase

Upvotes: 1

ashish_k
ashish_k

Reputation: 1581

Using sed:

sed -r -n 's/^([a-zA-Z])(.*)(\s+)([a-zA-Z])(.*)$/\U\1\L\2\3\U\4\L\5/p' file_name

Output:

Abcde Xyag

Upvotes: 0

Related Questions