2017-04-05 55 views
0

我必須從文件名去掉擴展名,而我是用這樣的:preg_replace函數猛砸相當於()

preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename) 

我想知道這個是什麼sed的等價物。 我目前的做法是這樣的:

$(echo $filename | cut -f 1 -d '.') 

,但它不工作的所有時間。

做正則表達式和sed有匹配相同的表達式和語法是不同或表情也隨之變化?

+0

最後的模式對於與SED HTTP bash的正則表達式://計算器.com/questions/14072592/replace-strings-using-sed-and-regex#14074855 – JustOnUnderMillions

+0

Extract filename&extention http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash#965072 – JustOnUnderMillions

+0

可能的重複[如何更改字符串中的擴展名與bash?](http://stackoverflow.com/questions/4411080/how-can-i-change-the-extension-name-in-a-字符串與-的bash) –

回答

2

這裏是切割使用bash文本的一些例子...

FileName="/var/www/html/index.html" 
echo "${FileName}" 
/var/www/html/index.html 

echo "${FileName%/*}" 
/var/www/html 

echo "${FileName##*/}" 
index.html 

TmpVal=$(echo "${FileName%.*}") 
echo "${TmpVal##*/}" 
index 

說明...

  • $ {變量%模式}刪除右邊
  • 第一圖案
  • $ {variable %% pattern}刪除右邊的最後一個圖案
  • $ {變量#模式}移除左側
  • $第一圖案{變量##模式}移除左側