Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
hai/hello/home/something/file.txt
file.txt
How to do that with grep?
#!/usr/bin/perl use File::Spec; use File::Basename; $n="hai/hello/home/something/file.txt"; my $m = basename $n; print "$m";
Add a comment
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
You can do it using sed:
sed
$ echo hai/hello/home/something/file.txt | sed "s|.*/||g" file.txt
or, easier, basename:
basename
$ basename hai/hello/home/something/file.txt file.txt
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.