Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

get the last string of a line

SOLVED
Go to Solution
Valued Contributor Valued Contributor

get the last string of a line

Hi all
I want to print the last string in a line.
If my path is "/var/opt/perf/parm" , I want to extract "parm", if "/var/stm/config/tools/monitor/sysstat_em.sapcfg", then only sysstat_em.sapcfg" should be extracted automatically.

I need only the last word in the line. How can I do this?
I look for some awk or sed commands.
5 REPLIES
Honored Contributor Honored Contributor

Re: get the last string of a line

Elif,
man basename should supply the answer

basename /var/opt/perf/parm would return parm
Honored Contributor Honored Contributor

Re: get the last string of a line

Hi,

try
# basename var/stm/config/tools/monitor/sysstat_em.sapcfg

regards,
John K.
it would be nice if you always got a second chance
Respected Contributor Respected Contributor

Re: get the last string of a line

Demat,

If I well understand :-) try with this :

echo "/var/stm/config/tools/sysstat_em.sapcfg"|awk -F\/ '{print $NF}'

It should always extract the last field of your string with the / as Field separator.

Hope this helps
Kenavo
Pat
Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
Valued Contributor Valued Contributor

Re: get the last string of a line

so I want a sed or awk command which deletes any prefix that ends with a / (slash)
Highlighted
Valued Contributor Valued Contributor

Re: get the last string of a line

thanks, basename and the awk was the hint I searched for.