LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-02-2012, 10:16 AM   #1
smks
LQ Newbie
 
Registered: Dec 2011
Posts: 6

Rep: Reputation: Disabled
Post output surname of a full name input BASH


echo please enter your full name
read name
~~~~~~~~~~<<<
echo you entered $surname

^^^^ how can i output the surname only

if i entered 'john douglas smith'

output smith

many thanks
shaun
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-02-2012, 10:45 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Well, you can do it with sed:

Code:
echo $name | sed 's/.* \([a-zA-Z]\+\)$/\1/'
This matches for anything followed by a space followed by one or more letters followed by an end-of-line, and replaces it with just the group of one or more letters

Hope this helps,
 
2 members found this post helpful.
Old 01-02-2012, 10:58 AM   #3
smks
LQ Newbie
 
Registered: Dec 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you v much
 
Old 01-02-2012, 12:34 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Here is a bash solution. It works similarly to the sed solution.

Code:
echo "${name##* }"
Here are some examples where it returns the surname as expected.

Code:
$ name='John Douglas Smith'; echo "${name##* }"
Smith
$ name='Mr. John Douglas Smith'; echo "${name##* }"
Smith
$ name='John Smith'; echo "${name##* }"
Smith
$ name='Smith'; echo "${name##* }"
Smith
Here are some examples where it fails to return the surname.

Code:
$ name='Smith, John'; echo "${name##* }"
John
$ name='John Douglas Smith MD'; echo "${name##* }"
MD
HTH
 
Old 01-03-2012, 04:12 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Telengard makes a good point - you're depending on it being entered in a specific way.

However, if you consider your problem solved, please marked the thread as 'SOLVED'

Thanks,
 
Old 01-03-2012, 04:29 PM   #6
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by Snark1994 View Post
you're depending on it being entered in a specific way.
I've watched people use programs I wrote for them. Even if you prompt them explicit instructions on what is expected, they will find ways to do the unexpected. Users are the most unpredictable component of any computing system.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Passing Output to another programs Input? zachet Programming 6 11-19-2009 11:12 AM
Pass output of one command as input to another on bash linuxlover.chaitanya Linux - Newbie 3 01-08-2009 02:46 AM
bash: INPUT/OUTPUT error when remoting in saanjac Linux - Newbie 2 08-08-2008 04:38 PM
bash input/output redirect problem greno Programming 2 12-31-2006 04:23 PM
bash: cd: /mnt/floppy/: Input/output error embsupafly Linux - Hardware 1 08-16-2003 07:36 AM


All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration