Awk Command

Hi, ive got this piece of script:

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

this doesn't mean you need to use awk. the "cut" command maybe enough for your needings. read the manpage of "cut" and tryout some examples...

try 'read first last', which will split the input on ' ', giving you the first word in $first and the rest of the input in $last

Hmmm, ive just asked a friend and he reckons its meant to be something like this:

echo "Please enter your full name:"
read name
echo "$name | awk'{print"Hello Mr.", $2}

This doesnt actually work, but am i on the right lines? im not trying to split a file, just the string.

Is that wrong then? Thanks v. much for the quick reply.

why are you tryin to use "awk" if other commands also work?

'read first last'? Sorry im new to this, could you explain a bit more pls?

The only reason im using awk is because i was advised that was the best way to do this..... Is there another easier way??

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...

Your approach is right. Just needs a little bit of tweaking in the awk syntax.

echo "Please enter your full name:"
read name
echo $name | awk '{print"Hello Mr." $2}'

that should be $0 instead of $2

My bad !

Last name is needed by OP. So, $2 should do good and not $0. Sorry.

Hi,

the awk command is very useful to print the last name. only the syntax matters. Jst try with this
echo $name | awk '{print "Hello Mr." $2}'

and that is what [ krishmaths ] posted already :slight_smile:

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.

When i use the code:

echo "Please enter your full name:"
read name
echo $name | awk '{print"Hello Mr." $2}'

And enter a first middle and surname e.g. john bloggs smith then it outputs 'Hello Mr. bloggs' instead of 'Hello Mr. smith'.

And if i use the code:

echo $name | awk '{print"Hello Mr." $0}'

And enter a first middle and surname e.g. john bloggs smith then it outputs 'Hello Mr. john bloggs smiths' instead of 'Hello Mr. smith'.

AHHH lol. Thanks very much for all your help

To print last name, try:

echo "Please enter your full name:"
read name
echo $name | awk '{print"Hello Mr. " $NF}'

Thanks PMM that worked great!

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....

Thanks for all your help everyone.

Phil

Hello kazazza

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}'

Thanks very much TheEngineer that worked perfectly.

And i thank all you that have helped me

Have a good New Year!

Try this:

echo "Please enter your full name: "
read name
echo $name | awk '{print $2}'

SINCE I CANNOT RECALL THE ABOVE MSG, PLS IGNORE IT.