0

I have a full path of a file say hai/hello/home/something/file.txt .How can I get file.txt as output eliminating full path?

How to do that with grep?

Abimaran Kugathasan
  • 29,154
  • 11
  • 70
  • 102
Stunner
  • 806
  • 1
  • 16
  • 35

3 Answers3

2
#!/usr/bin/perl
use File::Spec;
use File::Basename;
$n="hai/hello/home/something/file.txt";
my $m = basename $n;
print "$m";
pkm
  • 2,595
  • 1
  • 26
  • 43
1

You don't strictly need grep for this, but if you insist, this should work:

grep -o -e "\w*\.\w*$"

Optionally, consider the command basename:

basename hai/hello/home/something/file.txt
Nikhil
  • 2,270
  • 12
  • 14
1

You can do it using sed:

$ echo hai/hello/home/something/file.txt | sed "s|.*/||g"
file.txt

or, easier, basename:

$ basename hai/hello/home/something/file.txt
file.txt
mvp
  • 102,692
  • 13
  • 114
  • 144