Remove unnecessary slashes from a given path with bash

前端 未结 8 1772
囚心锁ツ
囚心锁ツ 2021-02-05 15:04

How can I get rid of unnecessary slashes in a given path?

Example:

p=\"/foo//////bar///hello/////world\"

I want:

p=\"/f         


        
8条回答
  •  隐瞒了意图╮
    2021-02-05 15:20

    This works with multiple separators and does not assume the given path should exist:

    p=/foo///.//bar///foo1/bar1//foo2/./bar2; 
    echo $p | awk '{while(index($1,"/./")) gsub("/./","/"); while(index($1,"//"))
         gsub("//","/");  print $1;}'
    

    But does not simplify well strings containing ".."

提交回复
热议问题