echo "Enter your full name" (i.e. first and last name)
read name
echo "Hello Mr.(last name only)
How do i split the string so that it says hello and then only displays the surname entered? The user has to be able to enter their name all on one line. i know im meant to use the awk command but im a noob at this. Any help you can give me would be very much appreciated. Thanks, Phil
as allready mentioned, the "cut" command is easy to use. and the use of 2 variables, as pludi described, is also very easy. if you have problems to understand specific things, just ask...
The 'read first last' is a good idea but no good if the user has a middle name. And i cant say 'read first middle last' because it wont work if the user doesn't have a middle name. Thanks for any suggestions.
I'm trying to improve this now, so that it doesn't greet everyone as "Mr." So I've made a variable called "title" (referring to Mr. Mrs. etc) my ONLY problem now is that i don't know how to order the command. This is what i have atm:
echo $name | awk '\{print"Hello $title " $NF\}'
I've tried putting the $title bit in different places, but no luck....
AWK need a mapping in order to read normal shell variables, you'd rather need to re-form your command as below to get the desired result:
title="Mr"
echo "Please Enter your name: "
read name
echo $name | awk -v awk_title=$title '{print "hello " awk_title ". " $NF}'
or you can assign the title variable from within AWK
echo "Please Enter your name: "
read name
echo $name | awk BEGIN '{title="Mr"} {print "Hello " title ". " $NF}'