Question

i just want to have the last word I have tried reverse too still I am not able to get the last word.

../init.d/halt
../init.d/killall
../init.d/watchdog
../init.d/iptables
../init.d/network
../init.d/evlogrmt
../init.d/evlog
../init.d/rdisc
../init.d/syslog-ng
../init.d/portmap
Was it helpful?

Solution

As I don't think it is possible to do a one-liner with cut, let's propose a grep solution:

$ grep -oE '[^/]+$' file
halt
killall
watchdog
iptables
network
evlogrmt
evlog
rdisc
syslog-ng
portmap

Of course, with awk it is pretty simple: awk -F/ '{print $NF}' file.

OTHER TIPS

This is what basename does:

$ xargs -I% basename % < file                                                  
halt
killall
watchdog
iptables
network
evlogrmt
evlog
rdisc
syslog-ng
portmap

awk version

$ awk -F "/" '{print $NF}' file
halt
killall
watchdog
iptables
network
evlogrmt
evlog
rdisc
syslog-ng
portmap

Another option (bash specific)

$ while read line; do echo "${line##*/}"; done < file
halt
killall
watchdog
iptables
network
evlogrmt
evlog
rdisc
syslog-ng
portmap

Use basename:

$ basename ../init.d/portmap

which returns:

portmap

I prefer basename and AWK

but you can also do it using sed, delete everything before / (including /) :

sed 's/.*\///'

To avoid the backslash, you can run sed in this way:

sed 's@.*/@@' file

or you can print anything after the last /

sed -r 's@.*/(.*)@\1@'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top