Initials of a name | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux 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.

Initials of a name

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-14-2012
deepwoodsv deepwoodsv is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 16 January 2014, 12:05 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Initials of a name

I'm stuck, so please tell me how to print the initials of a name (for ex E C for Eric Cartman).
If you could suggest a website related to string handling then that would be much appreciated too.
Thanks!
Sponsored Links
    #2  
Old Unix and Linux 01-14-2012
Aqualung71 Aqualung71 is offline
Registered User
 
Join Date: Nov 2011
Last Activity: 15 February 2015, 3:53 PM EST
Location: Karlsruhe, Baden-Wuerttemberg north of Black Forrest
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
just delete all uncapitalised characters:


Code:
echo "Eric Cartman" | sed "s/[a-z]//g"

Sponsored Links
    #3  
Old Unix and Linux 01-14-2012
deepwoodsv deepwoodsv is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 16 January 2014, 12:05 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks. But that seems like some sort of shortcut for the actual problem. If the name is given all lowercase then what? Dont consider it a name at all actually. Just print first letter of all words in an input line. It would be a shell script that way.
Anyone?
    #4  
Old Unix and Linux 01-14-2012
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 506 Times in 485 Posts
It would be nice to know what context you are trying to do this in. As has been suggested, if you have a file of first and last names, just run a sed across the file. However, if you have a string, or two tokens, as a part of a loop in a programme, then the answer will be different. As an example, if you are working in ksh or bash this is much more efficient than running sed for each individual string:


Code:
full_name="Eric Clapton"
initials="${full_name//[a-z]/}"
echo "$full_name -> $initials"

If you need E.C. or E. C. you can do something like this:


Code:
echo "${initials/ /.}."
echo "${initials/ /. }."

---------- Post updated at 11:23 ---------- Previous update was at 11:16 ----------

Crossed posts with you...

Again, if you're using a Ksh like shell language, then you could just echo out the first characters like this:


Code:
full_name="Eric Clapton"
last="${full_name#* }"
echo  "${full_name:0:1} ${last:0:1}"

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



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