bash: extracting last two dirs for a pathname

前端 未结 7 958
灰色年华
灰色年华 2021-02-12 12:42

I seem to have failed at something pretty simple, in bash. I have a string variable that holds the full path to a directory. I\'d like to assign the last two di

相关标签:
7条回答
  • 2021-02-12 13:29
    DIRNAME="a/b/c/d/e"
    DIRNAME2=`echo $DIRNAME | awk -F/ '{print $(NF-1)"_"$(NF)}'`
    
    DIRNAME2 then has the value d_e
    

    Change the underscore to whatever you wish.

    0 讨论(0)
提交回复
热议问题