Operating System - HP-UX
1753767 Members
5575 Online
108799 Solutions
New Discussion юеВ

print the last character of a string using awk

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

print the last character of a string using awk

Hi all,


how can I use awk to print the last character of a given string?

I need this to determine what status a server is being reported ie:

server1
server2
server3

if String = 1 standby
if String = 2 active
if String = 3 unknown

so it is important that the last char is printed.

many thanks

Chris.
hello
5 REPLIES 5
Mark McDonald_2
Trusted Contributor

Re: print the last character of a string using awk

If server1 server2 etc are the actual names:

awk -F"server" '{ print $2 }'
Mark McDonald_2
Trusted Contributor

Re: print the last character of a string using awk

you could also remove "server" with sed:

sed 's/server//'
James R. Ferguson
Acclaimed Contributor
Solution

Re: print the last character of a string using awk

Hi Chris:

# X="a b c d e f"
# echo $X|awk '{print substr($0,length($0),1)}'
f

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: print the last character of a string using awk

thanks Both,

James solution works the best for me :-)

Chris.
hello
lawrenzo_1
Super Advisor

Re: print the last character of a string using awk

ty
hello