Unix/Linux Go Back    

Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

only uppercase first character?

Shell Programming and Scripting

Closed Linux or Unix Question    
Thread Tools Search this Thread Display Modes
Old Unix and Linux 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?
Old Unix and Linux 05-22-2007
Shell_Life's Unix or Linux Image
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 $$*

Old Unix and Linux 05-22-2007
ghostdog74 ghostdog74 is offline
Registered User
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 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 Unix and Linux 05-22-2007
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Join Date: Jan 2007
Last Activity: 16 September 2015, 3:37 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 627 Times in 584 Posts

ex - infile<<!

Old Unix and Linux 05-23-2007
drl's Unix or Linux Image
drl drl is offline Forum Advisor  
Registered Voter
Join Date: Apr 2007
Last Activity: 1 December 2015, 8:58 PM EST
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,840
Thanks: 79
Thanked 262 Times in 236 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
Old Unix and Linux 05-23-2007
reborg's Unix or Linux Image
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 12 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
Old Unix and Linux 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'

Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:20 PM.