Operating System - Linux
1752770 Members
4932 Online
108789 Solutions
New Discussion юеВ

Re: get the last string of a line

 
SOLVED
Go to solution
Elif Gius
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 5
Peter Godron
Honored Contributor
Solution

Re: get the last string of a line

Elif,
man basename should supply the answer

basename /var/opt/perf/parm would return parm
john korterman
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
Patrice Le Guyader
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.
Elif Gius
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)
Elif Gius
Valued Contributor

Re: get the last string of a line

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