Pattern matching with grep

This question already has an answer here:

  • Extract filename and extension in Bash 35 answers

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

    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 :

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

    or, easier, basename :

    $ basename hai/hello/home/something/file.txt
    file.txt
    
    链接地址: http://www.djcxy.com/p/57090.html

    上一篇: 获取文件的扩展名

    下一篇: 与grep匹配的模式