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
  • 31,165
  • 11
  • 75
  • 105
Stunner
  • 961
  • 2
  • 19
  • 39

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,683
  • 2
  • 29
  • 44
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,298
  • 13
  • 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
  • 111,019
  • 13
  • 122
  • 148