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?

1
  • 1
    Why does it have to be with grep? Commented Oct 3, 2013 at 9:31

3 Answers 3

2
#!/usr/bin/perl
use File::Spec;
use File::Basename;
$n="hai/hello/home/something/file.txt";
my $m = basename $n;
print "$m";
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Please leave a comment if down-voting, explaining why. Otherwise it helps nobody.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.