How to print last word of line

Hi,

How to print last word of line?

#!/bin/bash

x1="This is Kiran"
echo "$x1"

how to print "Kiran" in new variable.i.e x2=kiran

x1="This is Kiran"
x2=`echo ${x1##* }`
echo $x2
Kiran
1 Like

Another way..

x1="This is Kiran"
x2=`echo $x1|cut -d ' ' -f3`
1 Like

There is always another way in Linux ::slight_smile:

x1="This is Kiran"
x2= `echo $x1 | awk '{ print $NF }'

1 Like

I guess no need of "echo" here.

x2=${x1##* }

Another way,

expr "$x1" : '.* \(.*\)'
2 Likes

Hi,

I have written one script :

#!/bin/bash

echo -n -e "\nEnter file name : "
read File
Temp=`find /apps12i -name $File`
echo "$Temp"

Output:

Enter file name : abc.log

/path1/abc.log
/path2/abc.log

I need script that should print "abc.log file present in multiple path"
so how do i compare "abc.log " ????

You should start new thread for different topic/issue.

Try something like...

Temp=`find /apps12i -name $File | wc -l`

if [ "$Temp" -gt 1 ]; then
 echo "abc.log file present in multiple path"
fi
1 Like

Ok.thanks