Get last field using awk substr

前端 未结 8 547
天涯浪人
天涯浪人 2020-11-29 02:20

I am trying to use awk to get the name of a file given the absolute path to the file.
For example, when given the input path /home/parent/child/filena

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 02:37

    It should be a comment to the basename answer but I haven't enough point.

    If you do not use double quotes, basename will not work with path where there is space character:

    $ basename /home/foo/bar foo/bar.png
    bar
    

    ok with quotes " "

    $ basename "/home/foo/bar foo/bar.png"
    bar.png
    

    file example

    $ cat a
    /home/parent/child 1/child 2/child 3/filename1
    /home/parent/child 1/child2/filename2
    /home/parent/child1/filename3
    
    $ while read b ; do basename "$b" ; done < a
    filename1
    filename2
    filename3
    

提交回复
热议问题