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
    #1  
Old 05-22-2007
fedora fedora is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 3 September 2013, 4:51 AM EDT
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.

abc
google
cnn
services

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
    #2  
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

Code:
## 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
    #3  
Old 05-22-2007
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
if you have python here's an alternative:

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

    #4  
Old 05-22-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 20 September 2014, 5:20 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,674
Thanks: 182
Thanked 620 Times in 578 Posts

Code:
ex - infile<<!
%s/^./\u&/|x
!

Sponsored Links
    #5  
Old 05-23-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 20 September 2014, 1:04 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,674
Thanks: 39
Thanked 193 Times in 176 Posts
Hi.

Also in perl:

Code:
#!/bin/sh

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

FILE=${1-data1}

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

exit 0

run on your sample in file data1 produces:

Code:
% ./s1
Abc
Google
Cnn
Services

cheers, drl
Sponsored Links
    #6  
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:


Code:
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
    #7  
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:


Code:
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 10:34 AM
Make the first character uppercase cola Shell Programming and Scripting 9 02-16-2011 07:44 AM
Uppercase/lowercase comparison of one character per line with awk?? ivpz Shell Programming and Scripting 10 01-08-2010 07:22 AM
uppercase to lowercase ajit.yadav83 UNIX for Dummies Questions & Answers 4 10-16-2008 11:36 AM
Converting to Uppercase dreams5617 Shell Programming and Scripting 3 11-12-2004 12:44 AM



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