user3278412 user3278412 - 1 year ago 79
Linux Question

Need the last entire word form each line from a file in Linux

i Have a file with content

./tomcat-6.0.16-catalina.jar
./tom7/tomcat-catalina-7.0.27.jar
./tomcat-catalina-8.5.5.jar
./tom8/tomcat-catalina-8.5.5.jar
./tomcat-catalina-7.0.27.jar


i want it in below format

tomcat-6.0.16-catalina.jar
tomcat-catalina-7.0.27.jar
tomcat-catalina-8.5.5.jar
tomcat-catalina-8.5.5.jar
tomcat-catalina-7.0.27.jar


how do i achieve this in linux?

Tom Tom
Answer Source
awk -F "/" '{print $NF}' file

Output:

tomcat-6.0.16-catalina.jar
tomcat-catalina-7.0.27.jar
tomcat-catalina-8.5.5.jar
tomcat-catalina-8.5.5.jar
tomcat-catalina-7.0.27.jar

-F "/" will use / as a field separator.

$NF is a built in awk variable for the number of fields, so this will print the last field.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download