only uppercase first character? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

only uppercase first character?

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 05-22-2007
fedora fedora is offline
Registered User
Join Date: Jul 2006
Last Activity: 4 January 2015, 4:22 AM EST
Posts: 114
Thanks: 1
Thanked 0 Times in 0 Posts
only uppercase first character?

should be a simple question, I am trying to uppercase every first character in a word on the list.


My first thought was sed 'y/^[a-z]/^[A-Z]/', but it changed all the characters, not just the first character.

any thoughts?
Sponsored Links
Old 05-22-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts

## Following extract the first initial character only:
sed 's/\(.\).*/\1/' input_file > $$FirstChar

## Following extract from the second character on:
sed 's/.\(.*\)/\1/' input_file > $$OtherChars

## Following changes the first character to upper case:
tr '[a-z]' '[A-Z]' < $$FirstChar > $$UpperChar

## Following paste together the upper case character and
## the other characters:
paste -d'\0' $$UpperChar $$OtherChars

## Following removes temporary files:
rm -f $$*

Sponsored Links
Old 05-22-2007
ghostdog74 ghostdog74 is offline
Registered User
Join Date: Sep 2006
Last Activity: 9 November 2014, 12:24 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
if you have python here's an alternative:

# echo "astring"  | python -c "print raw_input().capitalize()"

Old 05-22-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Join Date: Jan 2007
Last Activity: 27 January 2015, 7:26 AM EST
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts

ex - infile<<!

Sponsored Links
Old 05-23-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
Join Date: Apr 2007
Last Activity: 27 January 2015, 8:37 AM EST
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,720
Thanks: 49
Thanked 209 Times in 190 Posts

Also in perl:


# @(#) s1       Demonstrate quickie perl for uppercase first character.


perl -wp -e '$_ = ucfirst' $FILE

exit 0

run on your sample in file data1 produces:

% ./s1

cheers, drl
Sponsored Links
Old 05-23-2007
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts
A more general version for one or more words on a line:

awk '{ for ( i=1; i <= NF; i++) 
           {   sub(".", substr(toupper($i),1,1) , $i)  }  
            print }' file

use nawk/gawk/mawk as appropriate if awk doesn't work
Sponsored Links
Old 09-26-2008
jaimono jaimono is offline
Registered User
Join Date: Sep 2008
Last Activity: 26 September 2008, 6:45 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Upcase each word in a file with sed

This is with GNU sed:

cat bla.txt | sed 's/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
making the first character of word using uppercase using awk and sed matt12 Shell Programming and Scripting 3 10-17-2011 11:34 AM
Make the first character uppercase cola Shell Programming and Scripting 9 02-16-2011 08:44 AM
Uppercase/lowercase comparison of one character per line with awk?? ivpz Shell Programming and Scripting 10 01-08-2010 08:22 AM
uppercase to lowercase ajit.yadav83 UNIX for Dummies Questions & Answers 4 10-16-2008 12:36 PM
Converting to Uppercase dreams5617 Shell Programming and Scripting 3 11-12-2004 01:44 AM

All times are GMT -4. The time now is 12:52 PM.