Re: get the last string of a line (178 Views)
Reply
Valued Contributor
Elif Gius
Posts: 178
Registered: ‎03-20-2002
Message 1 of 6 (178 Views)
Accepted Solution

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.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 2 of 6 (178 Views)

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
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 3 of 6 (178 Views)

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
Patrice Le Guyader
Posts: 185
Registered: ‎01-01-2003
Message 4 of 6 (178 Views)

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
Elif Gius
Posts: 178
Registered: ‎03-20-2002
Message 5 of 6 (178 Views)

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)
Valued Contributor
Elif Gius
Posts: 178
Registered: ‎03-20-2002
Message 6 of 6 (178 Views)

Re: get the last string of a line

thanks, basename and the awk was the hint I searched for.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.